@msuman
Developer
Nothing here yet.
Nothing here yet.
No blogs yet.
Tapas Adhikary, To catch test function errors gracefully, I am using catch callback but I thought then callback is not needed because I am not waiting for test function execution and also not returning anything from API call/from the test function. Note: As the request dependency does not support async/await, I had to use promise constructor.
Really Nice article !! I will take this opportunity to clear my doubts. I code as per below. const request = require('request'); const test = userData => { console.log('test::parameters', userData); if (userData && userData.request === true) { const option = { method: 'POST', url: 'https://jsonplaceholder.typicode.com/posts', headers: { 'Content-type': 'application/json; charset=UTF-8' }, json: true, proxy: null, body: { title: 'foo', body: 'bar', userId: 1 } }; console.log('test::outgoingBody::', option); return new Promise((resolve, reject) => { request(option, (error, response) => { if (error) { return reject(error); } else { if (response && response.body ) { return resolve(); } else { return reject(new Error('Error Response')); } } }); }); } else { return Promise.reject(new Error('Invalid Userdata')); } }; //some business logics test().catch(err=>console.error(err)) //some business logics //-------------------------- //some business logics test({request:true}).catch(err=>console.error(err)) //some business logics As I am not returning anything from test function, I don't use then callback. I use Promise.reject to throw any synchronous error. Am I doing correctly?