A
A
Alexander Kositsyn2018-01-31 21:00:17
JavaScript
Alexander Kositsyn, 2018-01-31 21:00:17

Are the two codes the same?

There is a code like this
promise.then(value=>value).then(value=> ...)

promise.then(value=>Promise.resolve(value)).then(value=> ...)

Do I understand correctly that if in the handler for a promise we simply return a value like this (value=> value), then a promise is returned which is automatically in the resolve state? which is what the Promise.resolve() function does - it returns an already resolved promise.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
youngmysteriouslight, 2018-02-01
@alex_keysi

If promiseresolved with a non-promise value value, then promise.then(value => value)and are promise.then(value => Promise.resolve(value))equivalent to promises in the above chain.
The statuses of these promises are the same.
If the promise resolves with the promise value value, then promise.then(value => value)both promise.then(value => Promise.resolve(value))are equivalent: they will return a promise that resolves with the value that resolves to value.
PS To be honest, I don't know how to make a promise resolve to a value that is itself a promise. A cursory lurk and SO suggest that this is not possible, unless we wrap the promise returned during resolution in an object ( link )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question