I know that't very silly, but how to start a promise chain? I have, for example,
var p = new Promise(function(resolve,reject) {
setTimeout(function() {
return resolve("Hi from promise after timeout");
},1000);
});
How to run it? It should be like that,
when(p)
.then(function(msg) {
console.log(msg);
})
.catch(function(error) {
console.error(error);
});
But when
is not defined.
as long as the sun rises at dawn each day. I promise to love you as long as you smile at me in your special way. I promise to give you some time on your own so to your own self be true. I promise to give you my help and support always, in all that you do.
Promises are basically javascript objects used asynchronously. In a synchronous execution, you don't need states, per se. Your code either successfully execute or fail to execute. In asynchronous code, we execute, wait for callbacks and decide if its success or failure and continue with the synchronous code execution.
It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
You just need to do:
p.then(function(msg) {
console.log(msg);
})
.catch(function(error) {
console.error(error);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With