-2

Any Glue. Please do not give example of SETTIMEOUT.

My supervisor said that it is unnecessary having more then() statement if the then()'s do not return any new promise.I could not get it completely, I have looked at Exploring ES6 but there is no very well example of my 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(() => {
        for (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)
        });
        for (let index = 0; index < objectKeys.length; index++)
            createObject(index);
        console.log(objectOfUsers)
    })
    .catch((err) => console.log('Error happened : ' + err));
Pavel_K
  • 10,748
  • 13
  • 73
  • 186
geek
  • 51
  • 9
  • 1
    So what is your question? Which part of your code is a problem? It seems you already use promise and it is asynchronous. – ZeroCho Aug 15 '17 at 07:14
  • 1
    The first then would be `.then((data) => { return splitString(data, ';') }) .then((bankLogs) => {`. After that, your code confuses me too much. Please explain what you want to do (include why you only take every 5th index e.g.) Also include some sampledata and the functions that you are using – baao Aug 15 '17 at 07:15
  • One more thing. Having more then is not unnecessary. Next `then` works even there is no return in previous `then`. – ZeroCho Aug 15 '17 at 07:16
  • I was used then() statements for several times but he said it is not necessary , if then() statements return a new promise. I could not get it . Furthermore, he said that you should use local variables instead of global variables within then() statements – geek Aug 15 '17 at 07:16
  • Try this code `Promise.resolve().then(function() { console.log('promise1'); }).then(function() { console.log('promise2'); });` and you will understand it. Maybe your supervisor is wrong. – ZeroCho Aug 15 '17 at 07:17
  • @phippu bankLogs is a global variable at the top of the code which is not seen here. – geek Aug 15 '17 at 07:18
  • Using local variables or global variables depends on the code. Using `backLogs` as a global variable doesn't have any problem. – ZeroCho Aug 15 '17 at 07:18
  • yess, I tried to explain it but he does not understand. Thank you very much for your kindly responses, ZeroCho. – geek Aug 15 '17 at 07:20
  • Your question body doesn't seem to have anything to do with the question title. – Bergi Aug 15 '17 at 07:26
  • @ZeroCho Using `backLogs` as a global variable does have the problem of [being a global variable](https://stackoverflow.com/q/10525582/1048572). – Bergi Aug 15 '17 at 07:29

1 Answers1

1

From your code snippet you implemented Promise chain where resolving one promise initializes and 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 you only wrote simple logic and there is no new promises: bankLogs = splitString(data, ';')

So It 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, ';')

    for (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)
    });
    for (let index = 0; index < objectKeys.length; index++)
        createObject(index);
    console.log(objectOfUsers)
})
.catch((err) => console.log('Error happened : ' + err));

If you use bankLogs inside Promise only, make it private.


Promise chain approach we use mostly for good code readability and handling one error callback for all promises.

Edit

You can pass value through Promise chain by using some 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));
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • 1
    I do not use bankLogs just in one promise, I used it in other parts of the code.Hence, I declared it as a global variable but supervisor said that instead of making it global use local variables and make it via promise chaining. But I could not any example, (actually all example consists SETTIMEOUT, but it is different than mmy situation.)By the way thank you very much for caring me. – geek Aug 15 '17 at 07:33