如何包装返回promise的deferred函数使外部函数返回实值而不是promise

时间:2022-05-01 19:37:13

I have deferred function which returns promise for example

我有延期函数,例如返回promise

function promiseAddition(x,y){
 var defer = deferred();
 // code
 returns defer.promise;
}

I need to to call this function inside outer function and returns result of addition (value not a promise). How to achieve this ? I am using deferred module from node.js.

我需要在外部函数中调用此函数并返回添加结果(值不是promise)。怎么做到这一点?我正在使用node.js中的延迟模块。

2 个解决方案

#1


3  

I need to to call this function [from somewhere] and return [a] result […] value [that is] not a promise

我需要调用这个函数[从某个地方]并返回[a]结果[...]值[即]不是一个承诺

You cannot. If a value is wrapped in a promise once, it and all the values depending on it will be wrapped in promises forever.

你不能。如果一个值包含在一个promise中,那么它和依赖它的所有值将永远包含在promise中。

If you are using promises to represent asynchronous results, then it simply is impossible to extract the value - which is not yet there.

如果您使用promises来表示异步结果,则根本不可能提取值 - 这还不存在。

Of course various types of promises do allow to extract values, but usually only under certain circumstances - for example, a callback in the future. However, for a transformation (as is an addition) you will need to return another promise. Use its then method for that purpose.

当然,各种类型的承诺都允许提取值,但通常仅在某些情况下 - 例如,将来的回调。但是,对于转换(作为补充),您将需要返回另一个承诺。为此目的使用其then方法。

#2


0  

If function you're wrapping is returning promise which has not been resolved yet, then as Bergi pointed you can't return resolved value directly, as value is not there yet.

如果你正在包装的函数返回尚未解决的promise,那么就像Bergi指出的那样,你不能直接返回已解析的值,因为值还没有。

However, if it's about wrapping function that returns promise which in all cases is resolved, then when using deferred, you can easily configure such wrapper:

但是,如果它是关于返回promise的包装函数,在所有情况下都会解析,那么当使用deferred时,您可以轻松配置这样的包装器:

var getOutOfPromise = function () {
  var value;
  resolvedPromise.done(function (result) { value = result; });
  return value;
};

Still, recommended way is not to unwrap promises, but work with them, pass them across the functions and let them unwrap values whenever they need. If you're working with implementation that has promise.done and provides sync access to resolved value, there's no added cost.

不过,推荐的方法不是解包promises,而是使用它们,将它们传递给函数,让它们在需要时解包值。如果您正在使用具有promise.done的实现并提供对已解析值的同步访问,则不会增加成本。

#1


3  

I need to to call this function [from somewhere] and return [a] result […] value [that is] not a promise

我需要调用这个函数[从某个地方]并返回[a]结果[...]值[即]不是一个承诺

You cannot. If a value is wrapped in a promise once, it and all the values depending on it will be wrapped in promises forever.

你不能。如果一个值包含在一个promise中,那么它和依赖它的所有值将永远包含在promise中。

If you are using promises to represent asynchronous results, then it simply is impossible to extract the value - which is not yet there.

如果您使用promises来表示异步结果,则根本不可能提取值 - 这还不存在。

Of course various types of promises do allow to extract values, but usually only under certain circumstances - for example, a callback in the future. However, for a transformation (as is an addition) you will need to return another promise. Use its then method for that purpose.

当然,各种类型的承诺都允许提取值,但通常仅在某些情况下 - 例如,将来的回调。但是,对于转换(作为补充),您将需要返回另一个承诺。为此目的使用其then方法。

#2


0  

If function you're wrapping is returning promise which has not been resolved yet, then as Bergi pointed you can't return resolved value directly, as value is not there yet.

如果你正在包装的函数返回尚未解决的promise,那么就像Bergi指出的那样,你不能直接返回已解析的值,因为值还没有。

However, if it's about wrapping function that returns promise which in all cases is resolved, then when using deferred, you can easily configure such wrapper:

但是,如果它是关于返回promise的包装函数,在所有情况下都会解析,那么当使用deferred时,您可以轻松配置这样的包装器:

var getOutOfPromise = function () {
  var value;
  resolvedPromise.done(function (result) { value = result; });
  return value;
};

Still, recommended way is not to unwrap promises, but work with them, pass them across the functions and let them unwrap values whenever they need. If you're working with implementation that has promise.done and provides sync access to resolved value, there's no added cost.

不过,推荐的方法不是解包promises,而是使用它们,将它们传递给函数,让它们在需要时解包值。如果您正在使用具有promise.done的实现并提供对已解析值的同步访问,则不会增加成本。