Javascript 将getJSON在'then'中的结果传递给下一个'then'?

Javascript 将getJSON在'then'中的结果传递给下一个'then'?,javascript,ecmascript-6,promise,Javascript,Ecmascript 6,Promise,以下代码有两个then()。在第一个then()中有一个$.getJson()。如何将$.getJson()的结果传递给第二个then()的参数x 由于$.getJSON是异步的,因此需要返回该函数的执行,以便将其异步执行返回到下一个链式方法。getJSON返回类似于承诺的值,因此不需要回调。接受的答案令人困惑,并建议需要回调和返回cookie MyPromise .then(function (i) { //in a .then you can return a new promise l

以下代码有两个
then()
。在第一个
then()
中有一个
$.getJson()
。如何将
$.getJson()
的结果传递给第二个
then()
的参数
x


由于
$.getJSON
是异步的,因此需要返回该函数的执行,以便将其异步执行返回到下一个链式方法。

getJSON返回类似于承诺的值,因此不需要回调。接受的答案令人困惑,并建议需要回调和
返回cookie

MyPromise
.then(function (i) {
  //in a .then you can return a new promise like value
  return $.getJSON('http://localhost:5000/api/cookie/'+i.address+'/articleid/'+id);
})
.then(x => {
  //you returned a promise like value before so here you'll get the
  //  resolve of that promise.
  console.log("need to get cookie here: "+x);
})
//since jQuery 3 the promise like are more compatible with native promises
//  you can use catch
.catch(err=>console.warn("something went wrong:",err));  

我想你的意思是
返回x抱歉,这会起作用,但是$.getJSON的回调实际上是冗余的,可能是重复的
MyPromise
.then(function (i) {
  instance = i;
  return $.getJSON('http://localhost:5000/api/cookie/'+i.address+'/articleid/'+id);
})
.then(x => {
  console.log("need to get cookie here: "+x); // x is undefined 
});    
MyPromise
.then(function (i) {
  //in a .then you can return a new promise like value
  return $.getJSON('http://localhost:5000/api/cookie/'+i.address+'/articleid/'+id);
})
.then(x => {
  //you returned a promise like value before so here you'll get the
  //  resolve of that promise.
  console.log("need to get cookie here: "+x);
})
//since jQuery 3 the promise like are more compatible with native promises
//  you can use catch
.catch(err=>console.warn("something went wrong:",err));