javascript - How to make this piece of code asynchronous via promises? -
any glue. please not give example of settimeout.
my supervisor said unnecessary having more then() statement if then()'s not return new promise.i not completely, have looked @ exploring es6 there no example of situation.
let readdata = new promise(function (resolve, reject) { fs.readfile('/home/geek/desktop/activity-logs.csv', 'utf8', (err, data) => { if (err) reject(); resolve(data); }); }); readdata .then((data) => { banklogs = splitstring(data, ';') }) .then(() => { (let index = 2; index < banklogs.length; index += 5) { objectofusers[banklogs[index]] = {} temporarysymbols.push(banklogs[index].concat(banklogs[index + 1])); } object.keys(objectofusers).foreach(function (element) { objectkeys.push(element) }); (let index = 0; index < objectkeys.length; index++) createobject(index); console.log(objectofusers) }) .catch((err) => console.log('error happened : ' + err));
from code snippet implemented promise chain resolving 1 promise initializes , returns new one.
something like:
let readdata = new promise(/* ... */); readdata.then((data) => { let promise2 = new promise(/* ... */); return promise2; }).then(() => { // ... }) .catch((err) => console.log('error happened : ' + err));
however after resolving readdata
wrote simple logic , there no new promises: banklogs = splitstring(data, ';')
so should be:
let readdata = new promise(function (resolve, reject) { fs.readfile('/home/geek/desktop/activity-logs.csv', 'utf8', (err, data) => { if (err) reject(); resolve(data); }); }); readdata.then((data) => { var banklogs = splitstring(data, ';') (let index = 2; index < banklogs.length; index += 5) { objectofusers[banklogs[index]] = {} temporarysymbols.push(banklogs[index].concat(banklogs[index + 1])); } object.keys(objectofusers).foreach(function (element) { objectkeys.push(element) }); (let index = 0; index < objectkeys.length; index++) createobject(index); console.log(objectofusers) }) .catch((err) => console.log('error happened : ' + err));
if use banklogs
inside promise only, make private.
promise chain approach use code readability , handling 1 error callback promises.
edit
you can pass value through promise chain using wrapper object or list of items (example):
'use strict'; var fs = require('fs'); let readdata = new promise(function (resolve, reject) { fs.readfile('/users/maxim/appsflyer/projects/zevel/file1.txt', 'utf8', (err, data) => { if (err) reject(); var obj = { data: data, banklogs: {} // in 1st promise initialize banklogs }; resolve(obj); }); }); // 2nd dummy promise let newreaddata = new promise(function (resolve, reject) { fs.readfile('/users/maxim/appsflyer/projects/zevel/file2.txt', 'utf8', (err, data) => { if (err) reject(); resolve(data); }); }); readdata.then((obj1) => { obj1.banklogs.value = "somevalue"; return newreaddata.then(function(data2){ obj1.data2 = data2; return obj1; }); }).then((datatotal) => { console.log(datatotal); }) .catch((err) => console.log('error happened : ' + err));
Comments
Post a Comment