Promise 等待解决而不返回 [英] Promise waits to get resolve without return

查看:80
本文介绍了Promise 等待解决而不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 nodejs/javascript 中的异步处理的理解是,如果在函数中处理异步调用,则它必须返回承诺或接受回调以进行链接以及等待异步调用完成.

My understanding of the async handling in nodejs/javascript is that if an async call is handled in a function it must return a promise or accept a callback for chaining as well as for waiting for the async call to get complete.

但我发现它没有,因为以下代码有效并等待所有承诺完成

But I just find out it does not, as the following code works and wait for all the promises to get completed

function handlePromise() {
  Promise.resolve('Hello Async').then(data => {
    Promise.resolve('Hello Async 2').then(data => {
      return delay(3000).then(() => console.log(data));
    });
    return delay(2000).then(() => console.log(data));
  });

  Promise.resolve('hello').then(data => console.log(data))
};

function delay(time) {
  return new Promise(resolve => setTimeout(resolve, time))
}

function handlePromise2() {
  handlePromise()
};

handlePromise2();

当我将承诺返回到最后时,它应该可以工作.

It should work when I return the promises up to the end.

function handlePromise() {
	return Promise.resolve('Hello Async').then(data => {
		return Promise.resolve('Hello Async 2').then(data => {
			return delay(3000).then(() => console.log(data));
		});
	}).then(() => {
		return Promise.resolve('hello').then(data => console.log(data))
	}).then(() => {
		return Promise.resolve('hello').then(data => console.log(data))
	});

};

function delay(time) {
	return new Promise(resolve => setTimeout(resolve, time))
}

function handlePromise2() {
	return handlePromise()
};

handlePromise2();

同样适用于回调

const fs = require('fs')
function handlePromise() {
    delay();
    console.log('sync')
};

function delay() {
    fs.writeFile('data.txt', 'Hello Async', () => console.log('done'));
}

handlePromise();

那么我在这里错过了什么?

So what's I am missing out here?

如果只是用承诺调用 then 就解决了承诺,那么如果我不需要解决的值,异步/等待的意义何在?

If just calling a then with the promise resolves the promise then whats the point of async/await if I dont need the resolved value?

推荐答案

如果我理解你的问题是正确的,你是说第一个片段不应该工作,因为它没有返回承诺,你问为什么它有效.

If i understand your question right, you're saying that the first snippet should not work because it's not returning the promises and you're asking why is it working.

简短回答:它真的不起作用,handlePromise2() 完成并返回,而无需等待承诺得到解决或拒绝.

Short answer: It's not really working, handlePromise2() finished and returned without waiting on the promises to get resolved or rejected.

长答案:这就像你去面包店要面包,但不是在要求后排队等候你离开,面包仍然被烘烤但随后它被扔掉,因为客户(在我们的例子中)那是 handlePromise2) 进行调用并假定工作已完成,毕竟它的作用域只是调用该函数.

Long answer: It's like you going to the bakery and asking for a bread, but instead of waiting in line after asking for it you leave, the bread still gets baked but then it gets thrown away because the client (In our case that's handlePromise2) made the call and assumed that the work is finished, after all its scope was to just call that function.

使用承诺是为了让调用该函数的客户端知道期待什么,所以在您要求面包之后,您将等待它完成,这称为承诺它不是实际价值(又名面包),而是对价值的承诺.这就是您在第二个代码段中所做的.

Promises are used so that the client that's calling the function knows to expect something, so after you'd ask for the bread you'd wait for it to be finished, and that's called a promise it's not the actual value (aka bread) but a promise of a value. That's what you're doing in the second snippet.

现在可怜的 handlePromise() 不知道如何处理食物

And now poor handlePromise() doesn't know what to do with the food

这篇关于Promise 等待解决而不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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