Javascript 承诺内的承诺:从子承诺返回变量的正确方法是什么?(JS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43895473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:13:56  来源:igfitidea点击:

Promise inside promise: what's the correct way to return a variable from the child promise? (JS)

javascriptpromise

提问by Valerio Marzulli

I've a function like this:

我有这样的功能:

function top() {

  //promise1
  ParentPromise({
    ...some code here...
  }).then(function() {


    //promise2
        ChildPromise({
          ..some code here...
        }).then(function(response) {
         var result = response.result.items;

        });

});

};

and i need to return result value in this way:

我需要以这种方式返回结果值:

var myresult = start();

How i can do that? THX

我怎么能这样做?谢谢

采纳答案by Katana314

The definition of promises is that you cannot literally assign resultto myresult. However, you can make myresulta promise that resolves directly to resultfor the caller, however many promises were used to get that. The basic idea is that inside of each function in your above block, you should be returning the next Promise in the chain. eg:

promises 的定义是你不能从字面上赋值resultmyresult. 但是,您可以为调用者做出myresult直接解析result为 的承诺,无论使用了多少承诺来获得它。基本思想是在上面块中的每个函数内部,你应该return在链中执行下一个 Promise。例如:

function top() {

  //promise1
  return ParentPromise({
    ...some code here...
  }).then(function() {


    //promise2
        return ChildPromise({
          ..some code here...
        }).then(function(response) {
         var result = response.result.items;
         return result;

        });

});

};

In the end, the code calling top()won't know or care that 1, 2, or 12 chained promises were used to get result. It will also be able to register an error callback in case any of those promises failed.

最后,代码调用top()不会知道或关心 1、2 或 12 个链接的 Promise 用于获取result. 如果这些承诺中的任何一个失败,它也将能够注册一个错误回调。

回答by Erik Terwan

The neatest way in my opinion is to return the promise and chain them 'down' instead of to the left, avoiding christmas tree-like callback hell.

在我看来,最好的方法是返回承诺并将它们“向下”而不是向左链接,避免圣诞树般的回调地狱。

function top() {
  //promise1
  return ParentPromise({
    ...some code here...
  }).then(function(parent) {
    //promise2
    return ChildPromise(parent.id)
  }).then(function(response) {
    // do something with `result`

    return response.result.items;
  });
}

top().then(function(items){
  // all done 
});

Edit:Or in ES6 / lambda notation;

编辑:或者在 ES6 / lambda 符号中;

function top() {
  return ParentPromise().then(parent => {
    return ChildPromise(parent.id)
  }).then(response => {
    return response.result.items
  })
}

top().then(items => {
  // all done 
})

Edit:Or using Async/Await;

编辑:或使用异步/等待;

async function top() {
  const parent = await ParentPromise()
  const child = await ChildPromise(parent.id)

  return child.result.items
}

top().then(items => {
  // all done 
})

回答by Rob Brander

The challenge here is that you're trying to use asynchronous code in a synchronous way. You will need to change the way you think when working with asynchronous code.

这里的挑战是您试图以同步方式使用异步代码。在处理异步代码时,您需要改变思考方式。

One way to solve this is by having top() return ParentPromise, and then set the variable myresult using the .then() of the return of that promise.

解决此问题的一种方法是让 top() 返回 ParentPromise,然后使用该承诺的返回值的 .then() 设置变量 myresult。

function top() {
  ...
  return ParentPromie;
}
var myresult = ''; // default value until you have resolved your promise
top().then(result => myresult = result);

However, for this to work, you'll need to add code to resolve your promise:

但是,要使其正常工作,您需要添加代码来解决您的承诺:

var ParentPromise = new Promise(function(resolve) { 
  ... some code...
  var ChildPromise = new Promise(function(){
    ...some code...
  }).then(function(response){
    resolve(response.result.items);
  });
});

回答by jimmont

Run the following code, expanding to fit your specific circumstances. Note how it illustrates returning Promises, adding logic at various points along the way, before, during and after handling various pending logic.

运行以下代码,扩展以适应您的特定情况。请注意它如何说明返回 Promise,在处理各种挂起逻辑之前、期间和之后的各个点添加逻辑。

function ParentPromise(){
  // add complexity here, always returning a promise(s)
  // ie Promise.all, new Promise, Promise.reject or whatever...
  // here for simplicity, just return one to resolve with a clear value
  // to see it at the other end
  return Promise.resolve({items:1234});
}
function start() {
  // any method that returns the first promise, kicking off the complexity
  return ParentPromise(/*
    infinitely complex promise-conflation (individual or Promise.all, etc)
    only requirement is that each ***return a promise***
    where each is resolved or rejected
  */)
  .then((result) => {
    // do something with `result`
    console.log(result.items);
    // note returning what we want to come out **next**
    return result;
  });

};

var myresult;

start()
.then((result)=>{ myresult = result; })
.finally(()=>{ console.log('myresult:',myresult); });