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

Javascript 承诺中的承诺:什么';从子承诺返回变量的正确方法是什么?(JS),javascript,promise,Javascript,Promise,我有这样一个函数: function top() { //promise1 ParentPromise({ ...some code here... }).then(function() { //promise2 ChildPromise({ ..some code here... }).then(function(response) { var result = response.resul

我有这样一个函数:

function top() {

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


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

        });

});

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

var myresult = start();

我怎么能做到?THX

在我看来,最整洁的方式是回报承诺,并将它们“向下”而不是向左,避免像地狱一样的圣诞树

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 
});
编辑: 或采用ES6/lambda表示法

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

top().then(items => {
  // all done 
})
编辑:或使用异步/等待

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

  return child.result.items
}

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

承诺的定义是,您不能直接将
result
分配给
myresult
。但是,您可以为调用者做出直接解析为
result
myresult
承诺,不管使用了多少承诺。其基本思想是,在上述块中的每个函数中,您应该
返回
ing链中的下一个承诺。例如:

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;

        });

});

};

最后,调用
top()
的代码将不知道或不关心使用了1、2或12个链式承诺来获得
结果。它还能够注册一个错误回调,以防这些承诺中的任何一个失败。

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

解决这个问题的一种方法是让top()返回ParentPromise,然后使用该promise返回的.then()设置变量myresult

function top() {
  ...
  return ParentPromie;
}
var myresult = ''; // default value until you have resolved your promise
top().then(result => myresult = result);
但是,要使其起作用,您需要添加代码来解决您的承诺:

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

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

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); });

start
top
的关系如何?是否希望
ParentPromise
解析为
result
?这是一个google api调用。是否存在获得“返回结果”的方法?我不知道,就像我理解的ok的全局变量可能重复一样,现在我在控制台中得到[object]。如何正确打印结果?@ValerioMarzulli我不知道该如何帮助,因为您的示例代码没有console.log(),所以我不知道您打算如何检索/使用结果。但是,一个例子是:
top().then(函数(结果){console.log(结果);}
。在您询问之前,不,您无法在同一函数调用期间获得
结果
——这就是为什么我们有承诺和回调。这意味着子承诺将无法解析,因此其随后甚至不会被调用,因此父承诺也无法解析,导致两个待定承诺的内存泄漏。最常见的方法是o编写异步代码,就像它是同步的一样,就是使用异步…是的,JavaScript承诺会这样工作。IMHO,你的是最好的解决方案。