Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 承诺:如何传递数据?_Javascript_Promise - Fatal编程技术网

Javascript 承诺:如何传递数据?

Javascript 承诺:如何传递数据?,javascript,promise,Javascript,Promise,我在努力理解承诺是如何起作用的。我知道这就像一个“链式回调”。。。或多或少但是是否可以将获得的值提取到外部值/数组/对象 Promise1() .then(return x) .then() .then() .then(function(x){ console.log(x); }); 如何将数据从第一个.then()传递到第四个.then() 任何帮助我理解承诺的努力都将不胜感激。1 2. 三, 四, 您可以这样做,但需要将x值从一个传递到另一个: Promise1

我在努力理解承诺是如何起作用的。我知道这就像一个“链式回调”。。。或多或少但是是否可以将获得的值提取到外部值/数组/对象

Promise1()
  .then(return x)
  .then()
  .then()
  .then(function(x){
    console.log(x);
  });
如何将数据从第一个
.then()
传递到第四个
.then()

任何帮助我理解承诺的努力都将不胜感激。

1 2. 三,

四,


您可以这样做,但需要将
x
值从一个
传递到另一个

Promise1()
    .then(function() {
        return x; // The "x" is wrapped in a resolved promise by "then method"
    })
    .then(function(x) { // The "then" pass to the fulfillment callback the value inside the promise taken in input, which is the "x" value
        return x; //Same as above, we return the "x" value to pass it to the next "then" function
    })
    .then(function(x) { // Same as above
        return x; // Same as above
    })
    .then(function (x) { 
        console.log(x); // Here you can access the "x" value and print it
    })
该方法返回一个承诺。它最多包含两个参数:用于承诺成功和失败案例的回调函数

因此,您可以使用许多
然后
,因为您需要返回
x
,并使用返回的
x
做一些事情。。。等等

代码示例:

let array=[1,2,3,4,5,6],
承诺1=x=>{
返回新承诺((解决、拒绝)=>{
//你的逻辑。。。
设置超时(()=>
log(`For item${x}结果:${msg}`);
array.forEach(x=>Promise1(x)
.然后(x=>x)
.然后(x=>x)
.然后(x=>x)
.然后(x=>x)
.然后(message=>logMessage(x,message))
.catch(原因=>logMessage(x,原因))

);
(返回x)
错误-
。然后
只接受函数作为参数-忽略非函数,有效地将传入的解析值传递给下一个
。然后
-承诺的确切语义。然后详细描述了使用
承诺
函数的目的是优雅地处理异步函数异步函数的示例有:
setTimeout
、ajax POST、写入数据库等。您的示例不需要承诺,因为它没有异步函数。这有意义吗?您看过上的StackOverflow文档页面了吗?您是对的。我将编辑示例“承诺”:P这让我很困惑:S编辑:我会检查StackOverflow文档这对我来说很重要(现在我无法测试它,但看起来不错)。将数据传递/存储到外部变量中是一种可行的方法吗?您可以这样做,但这不是正确的做法,如果外部变量因副作用而被修改,可能会非常危险。您通常将
x
从第一个
传递到第二个
,然后
传递到第二个
x
,使用它来构建
y
并重新生成然后第三个
然后
y
并用它构造返回的
z
,依此类推……那么,我如何将最后一个“.Then()”中的数据返回到另一个函数?这让我很困惑最后一个
。然后()
将返回一个承诺。您可以将此承诺返回给另一个函数,该函数可以访问此承诺中的值,并对其调用
。然后()
。因此“.then()”将返回一个承诺…我需要使用other解决此问题。然后()”?
doSomething().then(function () {
  doSomethingElse();
}).then(finalHandler);

doSomething
|-----------------|
                  doSomethingElse(undefined)
                  |------------------|
                  finalHandler(undefined)
                  |------------------|
doSomething().then(doSomethingElse())
  .then(finalHandler);

doSomething
|-----------------|
doSomethingElse(undefined)
|---------------------------------|
                  finalHandler(resultOfDoSomething)
                  |------------------|
doSomething().then(doSomethingElse)
  .then(finalHandler);

doSomething
|-----------------|
                  doSomethingElse(resultOfDoSomething)
                  |------------------|
                                     finalHandler(resultOfDoSomethingElse)
                                     |------------------|
Promise1()
    .then(function() {
        return x; // The "x" is wrapped in a resolved promise by "then method"
    })
    .then(function(x) { // The "then" pass to the fulfillment callback the value inside the promise taken in input, which is the "x" value
        return x; //Same as above, we return the "x" value to pass it to the next "then" function
    })
    .then(function(x) { // Same as above
        return x; // Same as above
    })
    .then(function (x) { 
        console.log(x); // Here you can access the "x" value and print it
    })