我一直在浏览async/await,经过几篇文章后,我决定自己测试一下.但是,我似乎无法理解为什么这不起作用:
async function main() {
var value = await Promise.resolve('Hey there');
console.log('inside: ' + value);
return value;
}
var text = main();
console.log('outside: ' + text);
Run Code Online (Sandbox Code Playgroud)
控制台输出以下内容(节点v8.6.0):
>外面:[对象承诺]
>里面:嘿那里
为什么函数内部的日志消息会在之后执行?我认为创建async/await的原因是为了使用异步任务执行同步执行.
有没有办法可以使用函数内部返回的值而不使用asyncafter await?
如何在蚂蚁设计中异步验证表单字段?
<FormItem>
{getFieldDecorator('zipcode', {
initialValue: `${customer && customer.zipcode ? customer.zipcode : ''}`,
rules: [
// { required: true, message: 'Please input your Zipcode' },
{ validator: this.handlezipCodeChange },
],
})(
<Input
prefix={
<Icon type="zipcode" style={{ color: 'rgba(0,0,0,.25)', visibility: 'hidden' }} />
}
type="number"
placeholder="Zipcode"
// onChange={this.handlezipCodeChange}
/>
)}
</FormItem>
Run Code Online (Sandbox Code Playgroud)
函数调用
handlezipCodeChange = (rule, value, callback) => {
if (!value) {
callback('Please input your zipcode');
}
if (value.length < 5) {
callback('Please enter minimum length of 5');
}
if (value.length > …Run Code Online (Sandbox Code Playgroud)