javascript - About how to write promise
PHPz
PHPz 2017-05-19 10:31:14
0
1
563

I’m not very familiar with how to writePromise. I hope I can encapsulate some common states ofrejectandresolve. How can I write it better?
And the followingwriteFileIs it better to write it directly as a normal function? Sorry for posting the long code, please help. . . . Thanks

PHPz
PHPz

学习是最好的投资!

reply all (1)
黄舟

Asynchronous operations are best to requirepromise或者Generator函数 或者是asyncfunctions

var readFile = function (fileName) { return new Promise(function (resolve, reject) { fs.readFile(fileName, function(error, data) { if (error) reject(error); resolve(data); }); }); };

Jiang Zi is simpler! Then put all the data processing logic inside the callingthen或者catch

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!