匿名异步函数调用

时间:2018-10-08 09:51:53

标签: javascript ecmascript-6

是否可以创建匿名异步功能?

当然,命名函数有效:

const test = async() => {
 await ... //fetch some data
}

但是未命名的异步函数的有效语法是什么?我们应该在这里使用IIFE吗?

3 个答案:

答案 0 :(得分:1)

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var countrySchema = new Schema({
        country_id:String,
        country_name:String
});
var countryArray = mongoose.model('countryArray',countrySchema);


var stateSchema = new Schema({
        state_id:String,
        state_name:String,
        country: { type: Schema.Types.ObjectId, ref: 'countryArray' },

});
var states = mongoose.model('states',stateSchema);

答案 1 :(得分:0)

async function() => {
   await ... 
}

答案 2 :(得分:0)

双向,

  1. 简单方法
!async function () {
    console.log("e",'yibu');
}();

(async  () => {
    console.log("e",'yibu');
})();

//maybe this is better then above
;(async function () {
    console.log("e",'yibu');
}());

//this is allmost same
;[ async function () {
    console.log("e",'yibu');
}()];
  1. 使用[then]这不是绝对的“匿名”
var x=async  () => 100;

x().then(
    e=>console.log({e})
);