'use strict';
Promise.resolve(() => 'John')
.then((args) => {
console.log(args);
throw new Error('ops')
})
.catch((ex) => {
console.log(ex)
})
.then(() => {
throw new Error('ups')
console.log('Doe')
})
I think console.log(args);
should output 'John'
, but when I run this code, the output is [ [Function] ]
So I am confused.
A Promise is a JavaScript object that links producing code and consuming code.
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.
Consuming a Promise We consume a promise by calling then() and catch() methods on the promise. For example, requesting data from an API using fetch which returns a promise. The successCallback is called when a promise is resolved. It takes one argument which is the value passed to resolve() .
load must return a Promise or thenable . The first call will initiate the async methods. Further calls need to return a Promise which can only resolve or reject AFTER the initial Promise has completed. Chaining the promises will only work if no values are passed to the success handler (The first .
"I Promise a Result!" A JavaScript Promise object contains both the producing code and calls to the consuming code: When the executing code obtains the result, it should call one of the two callbacks: The Promise object supports two properties: state and result. While a Promise object is "pending" (working), the result is undefined.
While a Promise object is "pending" (working), the result is undefined. When a Promise object is "fulfilled", the result is a value. When a Promise object is "rejected", the result is an error object. You cannot access the Promise properties state and result. You must use a Promise method to handle promises.
Implementing a single Promise is pretty straightforward. In contrast, Chained Promises or the creation of a dependency pattern may produce “spaghetti code”. The following examples assume that the request-promise library is available as rp.
The function that encompasses the await declaration must include the async operator. This will tell the JS interpreter that it must wait until the Promise is resolved or rejected. The await operator must be inline, during the const declaration. This works for reject as well as resolve. Implementing a single Promise is pretty straightforward.
Promise.resolve
will create a new Promise resolved with the value you pass to it. So, in your case, your promise is actually resolved with the function object. It means that, the then
handler is passed the function object itself.
What you should have done is
new Promise((resolve, reject) => resolve('John'))
.then(args => {
console.log(args);
throw new Error('ops')
})
.catch(console.log.bind(console));
Now, you are creating a Promise
object and you are resolving that with the value John
.
If you want your Promise to be resolved with a value readily, then don't pass the function object but pass the actual value itself to Promise.resolve
function.
Promise.resolve('John')
.then(args => {
console.log(args);
throw new Error('ops')
})
.catch(console.log.bind(console));
Now, you have a Promise, resolved with value John
and the then
handler will get the resolved value John
.
Note: This is the recommended way of creating a Promise when you know the actual way to resolve with readily, so that you can avoid the Promise constructor anti-pattern.
'use strict';
Promise.resolve('John')
.then((args) => {
console.log(args);
throw new Error('ops')
})
.catch((ex) => {
console.log(ex)
})
.then(() => {
throw new Error('ups')
console.log('Doe')
})
I modify Promise.resolve('John')
, it works.
Please see the Promise.resolve.
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