1

I'm debugging my code and for some reason, the code execution stops after assigning promise.all to a variable, it doesn't execute the lines following. I am also getting errors such as

(node:5) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 17): undefined

though I am not sure if it is related.

It prints the first console.log, but not the last one or anything in between.

here is the problematic section.

console.log('this prints');

result = promise.all([dbinfo, vaultinfo])
.then(array =>{
    console.log('recieved all info');
    [dbinfo, vaultinfo] = array;
    mark(id);
    info = {
        id: id,
        username: dbinfo.username,
        country: dbinfo.country,
        firstname: vaultinfo.firstname,
        lastname: vaultinfo.lastname,
        email: vaultinfo.email
    };
    console.log('logging final info :' , info);
    return ( info );
})

.catch( (e) => {
    console.log('somethingwent wrong');
    console.error(e.message);
    return Promise.reject('Error');
    });

console.log('this doesnt');

console.log('final results', {result});

return result;

Here is the full code.

let central = require('./central'),
    db1 = require('./db1'),
    db2 = require('./db2'),
    db3 = require('./db3'),
    vault = require('./vault'),
    mark = require('./mark');

module.exports = function(id) {
    // TODO
    // Reminder: The deadline is tomorrow !

    // to find db, username, and country
    var dbinfo = central(id);

    dbinfo
    .then(data => {
        console.log({data});
        return data ;
    })

    .catch( ()=> {
        console.log('dbinfo resolution problem:');
        return Promise.reject('Error central');
        })

    .then(dbname=> {
        console.log('checking for db');
        switch (dbname){
            case 'db1': var db = db1(id); break;
            case 'db2': var db = db2(id); break;
            case 'db3': var db = db3(id); break;
            default:   
                console.log('error db not in file');
                Error('Database not in file');
        }
        
        db // is a promise, if rejected return name
        .catch(()=>{
            console.log(dbname +' database returned an error');
            throw 'Error '+dbname;
        })



        console.log('db found, at ' + dbname,  db);
        return db ;
    })

 

    vaultinfo = vault(id);

    vaultinfo
    .catch( () => {return Promise.reject('Error vault')});

    console.log('this prints');

    result = promise.all([dbinfo, vaultinfo])
    .then(array =>{
        console.log('recieved all info');
        [dbinfo, vaultinfo] = array;
        mark(id);
        info = {
            id: id,
            username: dbinfo.username,
            country: dbinfo.country,
            firstname: vaultinfo.firstname,
            lastname: vaultinfo.lastname,
            email: vaultinfo.email
        };
        console.log('logging final info :' , info);
        return ( info );
    })

    .catch( (e) => {
        console.log('somethingwent wrong');
        console.error(e.message);
        return Promise.reject('Error');
        });

    console.log('this doesnt');

    console.log('final results', {result});

    return result;
 
};
peng Jack
  • 305
  • 1
  • 3
  • 8

2 Answers2

0

You can't do just result = Promise.all(), you'll need to either use await, e.g.

const result = await Promise.all()...

or else keep chaining the promises -

Promise.all(...).then((result) => { /* ...do something with result */ })
andy mccullough
  • 9,070
  • 6
  • 32
  • 55
  • Sorry, what do you mean by that, I am new to promises. Wouldnt `result = Promise.all() ` return the same as await `as they both return a promise`? or would results without the `await` execute before the result is returned with a pending promise object? – peng Jack Sep 30 '20 at 21:55
  • think of a promise as a bucket that will _eventually_ have something inside it that you can lift out and use. `Promise.all()` returns you the promise, but there is nothing in it yet, it's the empty bucket, it hasnt _resolved_ (or _rejected_). `await` (or `then()`) is how you wait and unpack the result, i.e. your bucket now has a value inside it and you can use it. – andy mccullough Oct 01 '20 at 09:12
  • " Wouldnt result = Promise.all() return the same as `await` as they both return a promise?" - no, `Promise.all()` returns a promise, `await` unpacks the value of a _resolved_ promise – andy mccullough Oct 01 '20 at 09:14
0

You can use asyc/await , or use then , but should take care to write Promise in sensitive case, it's "Promise.all()" not promise.all()

let central = require('./central'),
    db1 = require('./db1'),
    db2 = require('./db2'),
    db3 = require('./db3'),
    vault = require('./vault'),
    mark = require('./mark');

module.exports = async function(id) {
    // TODO
    // Reminder: The deadline is tomorrow !

    // to find db, username, and country
    var dbinfo = central(id);

    dbinfo
    .then(data => {
        console.log({data});
        return data ;
    })

    .catch( ()=> {
        console.log('dbinfo resolution problem:');
        return Promise.reject('Error central');
        })

    .then(dbname=> {
        console.log('checking for db');
        switch (dbname){
            case 'db1': var db = db1(id); break;
            case 'db2': var db = db2(id); break;
            case 'db3': var db = db3(id); break;
            default:   
                console.log('error db not in file');
                Error('Database not in file');
        }
        
        db // is a promise, if rejected return name
        .catch(()=>{
            console.log(dbname +' database returned an error');
            throw 'Error '+dbname;
        })



        console.log('db found, at ' + dbname,  db);
        return db ;
    })

 

    vaultinfo = vault(id);

    vaultinfo
    .catch( () => {return Promise.reject('Error vault')});

    console.log('this prints');

    result = await Promise.all([dbinfo, vaultinfo])
    .then(array =>{
        console.log('recieved all info');
        [dbinfo, vaultinfo] = array;
        mark(id);
        info = {
            id: id,
            username: dbinfo.username,
            country: dbinfo.country,
            firstname: vaultinfo.firstname,
            lastname: vaultinfo.lastname,
            email: vaultinfo.email
        };
        console.log('logging final info :' , info);
        return ( info );
    })

    .catch( (e) => {
        console.log('somethingwent wrong');
        console.error(e.message);
        return Promise.reject('Error');
        });

    console.log('this doesnt');

    console.log('final results', {result});

    return result;
 
};
``
Aian
  • 226
  • 3
  • 10