代码之家  ›  专栏  ›  技术社区  ›  myol

同步使用已解决的承诺数据

  •  3
  • myol  · 技术社区  · 7 年前

    我正在学习有关承诺的知识,我绝对想在继续之前确保我了解它们的用法。我正在使用一个图书馆提供在线服务,它具有返回承诺的功能。

    我读到的几乎所有例子要么使用链接中的解析数据 then() 功能

    const result = Library.functionReturningAPromise()
    result.then(function(res) {
        const obj = new Example(res)
        return obj
    }).then(function(ob) {
        // do the rest of the logic within these then() functions
    })
    

    或在 async 功能

    async function test() {
        const result = await Library.functionReturningAPromise()
        const obj = new Example(result)
    
        // do the rest of the logic
    }
    

    我想知道是否有任何方法可以使用“正常”同步代码中已解决的承诺中的数据

     const result = Library.functionReturningAPromise()
    
     // do something to resolve the promise
    
     const obj = new Example(result)
    

    或者如果你需要一直“包装” 全部的 您的逻辑使用来自已解决承诺的数据 异步的 功能。

    2 回复  |  直到 7 年前
        1
  •  1
  •   jfriend00    7 年前

    我想知道是否有任何方法可以使用“正常”同步代码中已解决的承诺中的数据

    在处理异步响应时,无法编写完全同步的代码。一旦任何操作是异步的,就必须使用异步技术处理响应,并且不能与之同步编程。你必须学会异步编程。

    您显示的两个选项( .then() async/await )你的两个选择是处理归还的承诺吗?

    或者,如果您需要在异步函数中始终“包装”使用已解析promise中的数据的所有逻辑。

    如果你想用 await 这样,您就可以编写处理承诺的同步查找代码,然后所有这些代码都必须位于 async 功能。而且,一旦您离开该函数的范围(比如想要返回一个值),您将从 异步的 职能和再次必须处理的承诺。

    这是不可能的。这只是一个必须在javascript中学习的东西。过了一会儿它就变成了第二天性。


    如你所知,你可以使用 异步的 等待 获得一些同步的逻辑流,但是在执行此操作时,有一些事情需要确保您理解。从你的例子来看:

    async function test() {
        const result = await Library.functionReturningAPromise()
        const obj = new Example(result);
    
        // do the rest of the logic
    }
    
    1. 用声明的所有函数 异步的 回报承诺。这是你从他们那里得到的唯一回报。如果调用者正在查找返回值,或者希望知道异步操作何时完成或正在查找错误,则他们必须使用返回的Promise 然后() , .catch() 或再次 等待 里面 异步的 功能。
    2. 如果一个您正在等待的承诺被拒绝,那么它实质上将抛出并中止函数的其余执行,然后返回拒绝返回的承诺。
    3. 如果您希望执行流在被拒绝的承诺上中止,并且调用方将处理拒绝,那么这一切都可以。
    4. 但是,如果打电话的人没有处理拒绝,就需要有人来处理。如果你在用 等待 你需要在本地处理拒绝,然后把它们包装起来 try/catch (与同步异常的语法非常相似)。

    下面是一个使用 尝试/捕捉 具有 等待 要在本地处理错误:

    async function test() {
        try {
            const result = await Library.functionReturningAPromise()
            const obj = new Example(result);
    
            // do the rest of the logic
        } catch(e) {
            // handle promise rejection here
        }
    }
    

    以下是调用方处理错误的示例:

    async function test() {
        const result = await Library.functionReturningAPromise()
        const obj = new Example(result);
    
        // do the rest of the logic
    }
    
    test().then(() => {
        console.log("all done");
    }).catch(err => {
        console.log(err);
    });
    
        2
  •  1
  •   FatalMerlin    7 年前

    如果您想“离开”异步上下文,您可以等待承诺实现,并且 then() 调用其中一个“常规”函数作为回调。

    我可以理解异步编码风格可能非常令人困惑,但实际上这正是 then(function () {}) 做。

    如果您更容易理解或使用这种编码风格,那么它在功能上是等效的。

    function first() {
        console.log("sync!");
        doAsync();
    }
    
    function doAsync() {
        console.log("now we're async!");
        Library.functionReturningAPromise().then(second);
    }
    
    function second() {
        console.log("sync again!");
        // ...
    }