I'm really new to Q
js library.
I need to write a prepare
function that may return value momentarily or may need to wait for promise:
var prepare = function() {
var result = {};
result.aaa = 1;
if (something) {
callToFunction().then(function () {
result.bbb = 2
})
}
if (something2) {
callToFunction().then(function () {
result.ccc = 2
})
}
// here I have code to define other properties
// in sync (like `aaa`) or async (like `bbb`) manner
return result; //I need to return promise here
}
prepare().then(function(result) {use result....})
can someone fix prepare
function so I can always safely use prepare().then
someone may think it is related to How to maintain a promise-like API in this case?
but it is not. Function is responsible to define result
object.
- first: define
aaa
property - second: in some cases function will define
bbb
property - third: define other properties in sync (like
aaa
) or async (likebbb
) manner