NodeJS http.request not returning data even after specifying return on the 'end' event

前端 未结 3 2182
鱼传尺愫
鱼传尺愫 2020-12-12 05:47

Basically I am trying to scrap some data from website and perform the DOM extraction, deletion and updation on a callback function binded to the \'end\' event of http.

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 06:31

    You cannot return a value from an asynchronous callback. Well, you can, but it most likely will get ignored and most certainly won't do what you want.

    You cannot even return a promise in that place. You can only resolve a promise where you now use the return statements. You need to return a promise from the main function and then resolve or reject the promise in your event handlers where you use returns now.

    For more info see those answers:

    • Return Promise result instead of Promise in Nodejs
    • Return value in function from a promise block
    • jQuery: Return data after ajax call success

提交回复
热议问题