2
Considering the example below:
function retornaValor (){
return promiseQueveioDeAlgumaLib.then(
function(oQueQueroRetornar){
return {
sucesso: true,
data : oQueQueroRetornar
}
},
function(opsAlgoErrado){
return {
sucesso : false,
data : opsAlgoErrado
}
}
)
}
console.log(retornaValor())
What must I do to make sure I actually have oQueQueroRetornar
or opsAlgoErrado
and not a pending Promise?
For the record, when I write a code that way inside Meteor.methods
it works exactly as I would like, IE, returns a value I return to the customer but out of methods
or in the client (browser, using or not any framework) I have is a pending Promise.
You simply cannot return from an asynchronous operation. I mean, asynchronous functions even return, but to no one.
– bfavaretto
I think you’re trying to avoid an asynchronous flow and you’re looking for a way to do synchronous is that it? In that case I think you really have to accept that some things are asynchronous... if that doesn’t explain the question more clearly.
– Sergio
Yes, I need it synchronous, the problem is that 101% of Npm libraries are asynchronous, which makes me hostage to Meteor/nodejs + Fibers/Future, which allow me to control that.
– rogeriojlle
You can pass a function of
callback
as a parameter ofretornaValor
, and instead of returning to make acallback({sucesso: true, data: oQueQueroRetornar});
, on the other side you would do so:retornaValor(function(response) { console.log(response); });
– JuniorNunes
Yes I can, but I wouldn’t, since my need is to returnValue() actually return a value
– rogeriojlle