cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 394 forks source link

Does function call also accept promises? #505

Closed konsultaner closed 6 years ago

konsultaner commented 6 years ago

Just a question. Does when/function::call also accept promises or is it only used to catch excaptions?

rkaw92 commented 6 years ago

According to the example, it does resolve promises before actually calling: https://github.com/cujojs/when/blob/master/docs/api.md#fncall

konsultaner commented 6 years ago

@rkaw92 I can only see this:

A parallel to the Function.prototype.call function, that gives promise-awareness to the function given as first argument.

which means that it converts a function call into a promise. but am not sure wheather this function may return a promise itself.

rkaw92 commented 6 years ago

Yup:

const fn = require('when/function');

function getNumberPromise() {
    return Promise.resolve(123);
}

fn.call(getNumberPromise).then(function(value) {
    console.log('value: %d', value);
})

It's like when.try in this regard, apparently.

konsultaner commented 6 years ago

@rkaw92 Thats right when.try is the same method and the doc say:

Calls f with the supplied arguments, returning a promise for the result. The arguments may be promises, in which case, f will be called after they have fulfilled. The returned promise will fulfill with the successful result of calling f. If any argument is a rejected promise, or if f fails by throwing or returning a rejected promise, the returned promise will also be rejected.

Seems like f may return a promise.