How to use .then() in node.js?

前端 未结 5 1495
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 09:02

I\'m a complete beginner in node.js. I\'ve just read we can use .then() function for executing several functions in particular order. I was going t

5条回答
  •  北海茫月
    2020-12-15 09:31

    the .then() function is used for the PROMISE (so for async function ) when you need to KNOW WHEN IT'S FINISHED (and it is finished ok .. or KO ) ..so you have

    MYASYNCFUNCTION().then(function(response({
    
     //do what you want when it's finish and everything ok .. with the result
    
    }).catch(function(error){
     // it's finshed but with error
    })
    

    IN your example .. you don't have .then() function cause they're simple function .. but if you want to have it (i don't know why . they've not async stuff inside .. but you can)

    so

    // install it via npm install promise

    var Promise = require('promise');
    
    function one(){
    var promise = new Promise(function (resolve, reject) {
      resolve('one');
      });
    });
    
    }
    

    and then

    one().then(function(resp){ console.log(resp) })
    

    Hope it helps you!!

提交回复
热议问题