使用异步函数
Meteor 现在使用Promise
API 来执行所有异步操作。
这意味着对于许多函数,例如Meteor.call
函数,您都有一个返回结果承诺的对应函数Meteor.callAsync
。
您可以使用 Meteor.promisify
函数将任何以回调作为最后一个参数的函数转换为 Promise。
例如,您可以使 Meteor.loginWithPassword
返回一个 Promise,如下所示
javascript
import { Meteor } from 'meteor/meteor';
loginWithPasswordAsync = Meteor.promisify(Meteor.loginWithPassword);
const login = async () => {
try {
await loginWithPasswordAsync('username', 'password');
console.log('Logged in');
} catch (error) {
console.error('Login failed', error);
}
};
对于一般的 Promise,您可以使用await
关键字等待 Promise 解析。
javascript
const delay = async () => {
console.log('Waiting...');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Done waiting');
};
await delay(); // it will wait for 1 second before logging 'Done waiting'
通常,您应该在async
函数内部await
Promise,但您也可以使用then
方法来处理 Promise 解析。
javascript
const delay = () => {
console.log('Waiting...');
return new Promise(resolve => setTimeout(resolve, 1000));
};
delay().then(() => console.log('Done waiting'));
console.log('End of the function'); // this will be logged before 'Done waiting'
使用await
关键字,代码将在继续执行之前等待 Promise 解析。
这对于 Meteor 方法至关重要,因为它们本质上是异步的。
javascript
const callMethod = async () => {
try {
const result = await Meteor.callAsync('myMethod', 'arg1', 'arg2');
console.log('Method result:', result);
} catch (error) {
console.error('Method error:', error);
}
};
await callMethod();
如 call x callAsync 中所述,您应该等待Meteor.callAsync
函数解析。
处理错误
使用await
时,可以使用try
/catch
块来处理错误。
过去,您必须传递一个回调来处理错误,但现在您可以使用catch
块来处理错误。
javascript
import { Meteor } from 'meteor/meteor';
Meteor.call('myMethod', 'arg1', 'arg2', (error, result) => {
if (error) {
console.error('Method error:', error);
} else {
console.log('Method result:', result);
}
});
现在,使用await
,您可以使用try
/catch
块来处理错误。
javascript
import { Meteor } from 'meteor/meteor';
try {
const result = await Meteor.callAsync('myMethod', 'arg1', 'arg2');
console.log('Method result:', result);
} catch (error) {
console.error('Method error:', error);
}
您还可以使用then
方法来处理 Promise 解析。
javascript
import { Meteor } from 'meteor/meteor';
Meteor.callAsync('myMethod', 'arg1', 'arg2')
.then(result => console.log('Method result:', result))
.catch(error => console.error('Method error:', error));
异步上下文
要使用 await,您需要在async
函数内部。
javascript
const myFunction = async () => {
console.log('Waiting...');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Done waiting');
};
如果没有async
关键字,您将收到语法错误。
javascript
const myFunction = () => { syntax error!
console.log('Waiting...');
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('Done waiting');
};
您的上下文必须为async
才能使用await
关键字。
例如,在 Meteor 2 中,这段代码可以工作
javascript
import { Meteor } from 'meteor/meteor';
const someFunction = () => {
Meteor.call('myMethod', 'arg1', 'arg2', (error, result) => {
if (error) {
console.error('Method error:', error);
} else {
console.log('Method result:', result);
// do something with the result
}
});
};
现在您需要将其设为async
才能使用await
关键字。
javascript
import { Meteor } from 'meteor/meteor';
const someFunction = async () => {
try {
const result = await Meteor.callAsync('myMethod', 'arg1', 'arg2');
console.log('Method result:', result);
// do something with the result
} catch (error) {
console.error('Method error:', error);
}
};