cujojs/when

Does function call also accept promises?

konsultaner opened this issue · 4 comments

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

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

@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.

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.

@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.