DEV Community

Mahin Tazuar
Mahin Tazuar

Posted on

Javascript Promise Basic Things

Promise-
Sometimes we needed asynchronous javascript behavior. suppose when we are want to get data from the internet server but we know if we want to get some data, it needs some time but javascript working synchronously it gives an error when it does not find any things.
promise just help to javascript asynchronous behavior like if have any true value it's going for resolve otherwise its return rejects call back function.I need to catch the resolved value you need to use then and if need to catch the reject message, need to use the catch function with an arrow function as a parameter.

// promise syntext
let x =10;
const promises = new Promise((resolve, reject) => {
    if (x ==10) {
        resolve('workin');
    }
    else {
        reject('working');
    }
});
promises.then((w) => {
    console.log(w); 
});
Enter fullscreen mode Exit fullscreen mode

Discussion (0)