同步使用已解决的承诺数据 [英] Using resolved promise data synchronously

查看:61
本文介绍了同步使用已解决的承诺数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习承诺,我绝对希望在继续之前先了解自己的用法.我正在使用一个用于在线服务的库,该库具有返回承诺的功能.

I'm learning about promises and I absolutely want to make sure I am understanding their use before I continue. I am using a library for an online service which has functions which return a promise.

我阅读的几乎所有示例都在链接的 then()函数

Nearly all examples I read either use the resolved data in chained then() functions

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
}

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

I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code

 const result = Library.functionReturningAPromise()

 // do something to resolve the promise

 const obj = new Example(result)

或者如果您需要始终包装" 全部,则您的逻辑将使用来自 async 函数中已解决的承诺的数据.

or if you need to always 'wrap' all your logic that uses the data from a resolved promise in an async function.

推荐答案

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

I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code

在处理异步响应时,无法编写完全同步的代码.一旦任何操作都是异步的,您就必须使用异步技术来处理响应,并且无法对其进行同步编程.您必须学习异步编程.

There isn't a way to write completely synchronous code when handling asynchronous responses. Once any operation is asynchronous, you have to deal with the response using asynchronous techniques and cannot program with it synchronously. You must learn to program asynchronously.

显示的两个选项( .then() async/await )是处理返回的诺言的两个选择.

The two options you show (.then() or async/await) are your two choices for handling the returned promise.

或者如果您需要始终包装"使用异步函数中已解决的promise中的数据的所有逻辑.

or if you need to always 'wrap' all your logic that uses the data from a resolved promise in an async function.

如果您想使用 await ,以便可以编写用于处理承诺的同步代码,那么所有这些代码都必须在 async 函数内部.而且,一旦您离开了该函数的范围(例如想要返回一个值),就从 async 函数返回了一个Promise,再次必须处理该Promise.

If you want to use await so that you can write synchronous-looking code for dealing with promises, then all that code has to be inside an async function. And, as soon as you leave the scope of that function (like want to return a value), you're returning a promise from the async function and again have to deal with the promise.

无法解决此问题.这只是必须用Java语言学习的东西.不久之后,它变成了第二天性.

There is no way around this. It's just something one must learn in Javascript. It becomes a second nature after awhile.

您似乎已经知道,可以使用 async await 来获得一些看起来很同步的逻辑流,但是有一些事情可以确保您在执行时了解这.从您的示例中:

As you appear to know, you can use async and await to get some synchronous-looking logic flow, but there are some things to make sure you understand when doing this. From your example:

async function test() {
    const result = await Library.functionReturningAPromise()
    const obj = new Example(result);

    // do the rest of the logic
}

  1. 所有用 async 声明的函数都返回一个承诺.那是您从他们那里获得的唯一一种返回值.如果调用方正在寻找返回值,或者想知道异步操作何时完成或正在寻找错误,则他们必须在 .then() .catch()或再次在 async 函数内使用 await .
  2. 如果您正在等待拒绝的诺言,它将本质上抛出并中止该函数的其余部分的执行,然后返回被拒绝的诺言.
  3. 如果您希望执行流程因被拒绝的诺言而中止而调用者将要处理拒绝,那么就可以了.
  4. 但是,如果呼叫者没有处理拒绝,则有人需要处理.如果您使用的是 await ,并且需要在本地处理拒绝,则需要将它们包装在 try/catch 中(语法与同步异常非常相似).
  1. All functions declared with async return a promise. That's the only kind of return value you get from them. If the caller is looking for a return value or wants to know when the asynchronous operations are done or is looking for errors, they MUST use that returned promise with .then(), .catch() or again with await inside an async function.
  2. If a promise that you are awaiting rejects, it will essentially throw and abort the rest of the execution of the function and then return reject the returned promise.
  3. If you want the execution flow to abort on a rejected promise and the caller is going to handle the rejection, then that's all fine.
  4. But, if the caller isn't handling the rejection, the somebody needs to. If you're using await and you need to handle rejections locally, then you need to wrap them in try/catch (very similar syntax to synchronous exceptions).

这是一个将 try/catch await 一起在本地处理错误的示例:

Here's an example of using try/catch with await to handle errors locally:

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

这是呼叫者处理错误的示例:

Here's an example of handling errors by the caller:

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

这篇关于同步使用已解决的承诺数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆