Node.js如何在异步方法都执行后再执行

福尔摩斯对我笑 2018-11-28 2747 次 2097条

参考链接:https://blog.csdn.net/duzixi/article/details/53842182

f.on("ready", function()
{
console.log("ready");
files.forEach( (elem,index) =>
{
var action = ()=>{
return new Promise( (resolve,reject )=>{

f.get('/www/pdf/msds/'+elem, function(err, stream){
if(err)
{
f.put(offlineFile +'\\'+elem, '/www/pdf/msds/'+elem, function (err) {
if (err) reject("文件上传出错:" + offlineFile + err);
console.log("成功新增");
resolve("新增");
})

}else
{

f.put(offlineFile +'\\'+elem, '/www/pdf/msds/'+elem, function (err) {
if (err) reject("文件上传出错:" + offlineFile + err);
console.log("成功覆盖");
resolve("覆盖");
})
}
})



})
}

actions.push(action()); //将每次循环调用的方法保存到数值
})

//数值中所有方法执行完毕后,调用
Promise.all(actions).then( (result)=>{
console.log(result)
f.end();
}).catch((error)=>{
console.log(error)
})
let arr = [1,2,3,4,5];

        (async () => {
            for (let i=0; i<arr.length; i++) {
                console.log(i);
                await fn();
            }
        })();

        arr.forEach( async v => {
            console.log(v);
            await fn();
        } );

        (async () => {
            for (let i of arr) {
                console.log(i);
                await fn();
            }
        })();
        
        function fn() {
            return new Promise( resolve => {
                setTimeout(() => {
                    console.log('...');
                    resolve();
                }, 1000);
            } );
        }
标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 2097 条评论 1


  1. boong

    I used to be able to find good information from your blog articles.

    回复 2020年2月26日
  2. Extension Cord Lengths

    I absolutely love your site.. Pleasant colors & theme.
    Did you create this web site yourself? Please reply back as I’m
    wanting to create my very own site and want to learn where you got this from
    or just what the theme is named. Thank you!

    回复 2020年2月26日
  3. motherfucker

    Oh my goodness! Incredible article dude! Thanks, However I am encountering difficulties with your RSS. I don’t understand the reason why I can’t subscribe to it. Is there anyone else having identical RSS problems? Anybody who knows the solution will you kindly respond? Thanks!!

    回复 2020年2月26日
  4. w88

    I don’t even understand how I stopped up right here, however I thought this post was great.
    I don’t know who you’re but certainly you’re going to a well-known blogger
    when you are not already. Cheers!

    回复 2020年2月26日
  5. Baltimore MD SEO

    This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Many thanks!

    回复 2020年2月26日
  6. orgasim

    I really love your site.. Great colors & theme. Did you build this website yourself? Please reply back as I’m trying to create my own site and want to know where you got this from or what the theme is called. Many thanks!

    回复 2020年2月26日
  7. slut

    You are so interesting! I don’t think I’ve truly read a single thing like this before. So nice to find someone with a few original thoughts on this topic. Really.. thank you for starting this up. This web site is something that is needed on the internet, someone with some originality!

    回复 2020年2月26日
  8. lắp đặt camera quận 1 2 3 4 5 6 7 8 9 10 11 12

    lắp đặt camera quan sát quận 1 2 3 4 5 6 7 8 9 10 11 12 –
    Thanks for ones marvelous posting! I genuinely enjoyed reading it, you may
    be a great author.I will be sure to bookmark your blog and
    definitely will come back very soon. I want to encourage one to continue your great work, have a
    nice day!

    回复 2020年2月26日
  9. asshole

    I have to thank you for the efforts you’ve put in penning this site. I am hoping to check out the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has inspired me to get my very own website now 😉

    回复 2020年2月26日
  10. date

    Hi! I could have sworn I’ve been to this blog before but after going through many of the articles I realized it’s new to me. Anyhow, I’m certainly pleased I came across it and I’ll be book-marking it and checking back frequently!

    回复 2020年2月26日
  11. anus

    The next time I read a blog, Hopefully it does not fail me as much as this particular one. I mean, Yes, it was my choice to read, but I genuinely believed you’d have something helpful to talk about. All I hear is a bunch of complaining about something that you could fix if you weren’t too busy looking for attention.

    回复 2020年2月26日
  12. retard

    Howdy! This blog post could not be written any better! Looking at this post reminds me of my previous roommate! He constantly kept preaching about this. I’ll send this information to him. Fairly certain he will have a good read. I appreciate you for sharing!

    回复 2020年2月26日
  13. vans shoes

    I precisely needed to thank you very much once again. I am not sure the things that I could possibly have undertaken without the actual smart ideas discussed by you on my problem. It had become the daunting dilemma in my position, however , viewing your specialised style you dealt with the issue took me to jump for joy. I am happy for this service and then pray you comprehend what an amazing job you are putting in training others using your webblog. I am sure you haven’t met all of us.

    回复 2020年2月26日
  14. tuan tender

    I wanted to thank you for this excellent read!! I definitely loved every bit of it. I have got you book marked to look at new stuff you post…

    回复 2020年2月26日
  15. live poker cash game strategy

    Fantastic website you have here but I was wanting
    to know if you knew of any forums that cover the same topics talked about here?

    I’d really love to be a part of community where I can get responses
    from other knowledgeable individuals that share the same interest.

    If you have any recommendations, please let me know. Thanks a lot!

    回复 2020年2月26日
  16. date

    This page really has all of the information I wanted concerning this subject and didn’t know who to ask.

    回复 2020年2月26日
  17. Abbruch aus Lübeck

    Howdy! This blog post could not be written any better! Looking at this article reminds me of my previous roommate! He always kept preaching about this. I am going to forward this article to him. Fairly certain he will have a great read. Many thanks for sharing!

    回复 2020年2月26日
  18. ass fuck

    Excellent article. I definitely love this site. Continue the good work!

    回复 2020年2月26日
  19. Gutter Repair Raleigh NC

    This page really has all the information I needed concerning this subject and didn’t know who to ask.

    回复 2020年2月26日
  20. katharine897.wordpress.com

    After exploring a handful of the blog articles on your web page, I honestly appreciate your way of writing a
    blog. I saved it to my bookmark site list and will be checking back soon. Please visit my web site too
    and tell me your opinion.

    回复 2020年2月26日
  21. bio cbd öl schweiz

    Simply desire to say your article is as surprising.

    The clearness for your submit is just cool and that i can think you are a professional in this subject.
    Fine along with your permission let me to clutch your RSS feed to
    keep updated with forthcoming post. Thank you 1,000,000 and please carry on the enjoyable work.

    回复 2020年2月26日
  22. coon

    An intriguing discussion is definitely worth comment. I think that you should write more on this subject, it might not be a taboo matter but typically people do not speak about these subjects. To the next! Cheers!!

    回复 2020年2月26日
  23. cockfucker

    Good article. I am dealing with a few of these issues as well..

    回复 2020年2月26日
  24. lesbian

    There is definately a lot to learn about this issue. I really like all of the points you’ve made.

    回复 2020年2月26日
  25. free porn

    Link exchange is nothing else but it is simply placing the other person’s webpage link on your page at suitable place and
    other person will also do similar in favor of you.

    回复 2020年2月26日
  26. Chatturb

    ここは仕事場じゃねーぞ?

    回复 2020年2月26日
  27. darn

    This excellent website really has all of the info I wanted about this subject and didn’t know who to ask.

    回复 2020年2月26日
  28. Lexus888 Sabung Ayam

    Hi would you mind sharing which blog platform you’re using?
    I’m planning to start my own blog soon but I’m having a difficult time making a decision between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m looking for something completely unique.

    P.S Apologies for being off-topic but I had to ask!

    回复 2020年2月25日
  29. 온라인 카지노 사이트

    Hi it’s me, I am also visiting this web site regularly, this site is truly good
    and the users are in fact sharing good thoughts.

    回复 2020年2月25日
  30. excavation company

    I’m not that much of a online reader to be
    honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later on. All the best

    回复 2020年2月25日
  31. King Sentinel

    Good web site you have got here.. It’s hard to find excellent writing like yours these days. I honestly appreciate individuals like you! Take care!!

    回复 2020年2月25日
  32. cream pemutih wajah yang aman

    This is my first time pay a quick visit at here and i am really
    pleassant to read everthing at alone place.

    回复 2020年2月25日
  33. Buy HCG

    Great article.

    回复 2020年2月25日
  34. modern construction technology

    It’s a pity you don’t have a donate button! I’d definitely donate to this fantastic blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to fresh updates and will share this site with my Facebook group. Talk soon!

    回复 2020年2月25日
  35. a325n tc bolts

    Hey just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Ie. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I figured I’d post to let you know. The design and style look great though! Hope you get the issue fixed soon. Many thanks

    回复 2020年2月25日
  36. homoerotic

    I seriously love your website.. Very nice colors & theme. Did you create this site yourself? Please reply back as I’m looking to create my very own website and would love to find out where you got this from or just what the theme is called. Thanks!

    回复 2020年2月25日
  37. This Site

    I must thank you for the efforts you have put in writing this site. I really hope to see the same high-grade blog posts from you later on as well. In truth, your creative writing abilities has inspired me to get my very own blog now 😉

    回复 2020年2月25日
  38. Teocycle 回复 2020年2月25日
  39. tc bolts

    Heya are using WordPress for your site platform? I’m new to the blog world but I’m trying to get started and set up my own. Do you need any html coding expertise to make your own blog? Any help would be greatly appreciated!

    回复 2020年2月25日
  40. 우리카지노

    Staying at this weekly resort in NYC could be a lifetime expertise for positive. http://www.super-bike.com/__media__/js/netsoltrademark.php?d=uricasino369.com/

    回复 2020年2月25日
  41. asshole

    The next time I read a blog, I hope that it won’t fail me just as much as this one. I mean, Yes, it was my choice to read through, however I truly thought you’d have something useful to talk about. All I hear is a bunch of whining about something you can fix if you were not too busy looking for attention.

    回复 2020年2月25日
  42. cockfucker

    I was extremely pleased to uncover this website. I wanted to thank you for your time for this particularly fantastic read!! I definitely liked every part of it and I have you book marked to look at new stuff on your website.

    回复 2020年2月25日
  43. dick

    Aw, this was an extremely good post. Finding the time and actual effort to make a very good article… but what can I say… I hesitate a lot and don’t manage to get anything done.

    回复 2020年2月25日
  44. joker123.dev

    ดูแก้เสี้ยนละกันหมดตูดแล้ว

    回复 2020年2月25日
  45. This site

    Everything is very open with a precise explanation of the challenges. It was truly informative. Your site is very helpful. Thank you for sharing!

    回复 2020年2月25日
  46. lotto vip

    Hi there, after reading this amazing piece of writing i am too delighted to
    share my experience here with colleagues.

    回复 2020年2月25日
  47. webcam

    I must thank you for the efforts you’ve put in penning this site. I really hope to see the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has motivated me to get my very own blog now 😉

    回复 2020年2月25日