难以理解承诺和asycn ...;<

发布于 2025-02-06 01:51:22 字数 423 浏览 1 评论 0原文

我正在学习REACT API。承诺和异步正在使用。而且...

我找到了Promise,异步MDN文档,但我不明白“在某种程度上始终返回承诺对象”

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

async function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}


process().then(() => {
    console.log('end')
})

I'm learning React API. Promise and async are using this. and...

I found promise, async MDN docs, but I can't understand "async in somefunction always return promise object"

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

async function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}


process().then(() => {
    console.log('end')
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ记忆︶ㄣ 2025-02-13 01:51:23

我们有承诺和可观察到处理函数或应用程序异步性质的承诺和可观察的性质,是的,是的,始终返回承诺,而同步和异步函数都可以返回obervables。承诺之后是两个关键字,

const promise = FooPromise((resolve,reject) = > {
revolve("i am resolve");
reject("i am reject");
)

此后,我们定义了如果分别由这些关键字解决或拒绝我们的承诺该怎么办。

.then((resolveMessage) => {
console.log(resolveMessage);
}
.catch((catchMessage) => {
console.log(CatchMessage);
}

We have Promise and Observables to deal with the asynchronous nature of functions or app and yes Async always returns a promise while obervables can be returned by both synchronous and Asynchronous functions. A promise is followed by two keywords,

const promise = FooPromise((resolve,reject) = > {
revolve("i am resolve");
reject("i am reject");
)

after this we define what to do if our promise is resolved or rejected by these keywords respectively.

.then((resolveMessage) => {
console.log(resolveMessage);
}
.catch((catchMessage) => {
console.log(CatchMessage);
}
阳光①夏 2025-02-13 01:51:23

当调用async函数时,它将返回Promise

如果异步函数返回一个值,则承诺的状态将通过返回的值解决。

如果异步函数引发异常或某些值,则承诺的状态将被拒绝,并以抛出值拒绝。

等待表达式可以在异步函数中使用,该函数将暂停异步函数的执行,并等待将承诺传递给表达式的解决方案。分辨率之后,它将返回解决值并继续执行异步函数。

When the async function is called, it returns a Promise.

If the async function returns a value, the state of the Promise will be resolved with the returned value.

If the async function throws an exception or some value, the state of the Promise will be a rejected with the thrown value.

An await expression can be used inside an async function, which will suspend the execution of the async function and wait for the resolution of the Promise passed to the expression. After the resolution, it will return the resolved value and continue the execution of the async function.

信仰 2025-02-13 01:51:23

通过async return Promise Object这意味着该功能现在返回承诺包装器。您可以将其视为临时占位持有人,而不是发送的实际值。承诺可以被拒绝或解决,直到诺言未完成之前,它将保持在待定状态。

这使得异步操作很容易发生,这可能需要一些时间并破坏正常的执行流。

另外,。然后语句与承诺一起使用。如果您尝试在没有异步的情况下访问,则会出现错误,因为它期望有望对象。

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}

process().then(() => {
    console.log('end')
})

By async return promise object it means the function now returns a promise wrapper. You can consider it as a temporary placeholder instead of the actual value sent. The promise can either be rejected or resolved and till the promise is not completed it will stay in pending state.

This allows for asynchronous operations to happen easily which might take some time and disrupt the normal execution flow.

Also, The .then statement works with promises. If you try to access without async it will be throw an error since it expects a promise object.

function sleep(ms){
    return new Promise( resolve => setTimeout(resolve, ms));
}

function process(){
    console.log('hi');
    await sleep(1000);
    console.log('hello');
}

process().then(() => {
    console.log('end')
})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文