Function not working from class

0 votes

I have this class:

class Blockchain {

    constructor() {

    }



    // Add new block

    async addBlock(newBlock) {

        var bla;

        this.getBlockHeightPromise().then(i => bla = i);

        console.log('bla: ' + bla);}

//Note addBlock needs to async because await db.createReadStream follows



    getBlockHeightPromise() {

        return new Promise(function (resolve, reject) {

            let i = 0;


            db.createReadStream()

                .on('data', function () {

                    i++;

                })

                .on('error', function () {

                    reject("Could not retrieve chain length");

                })

                .on('close', function () {

                    resolve(i);

                });

        })

    }

}


Now when I try to run it, it doesn’t work:

let blockchain = new Blockchain();

blockchain.addBlock(someBlock)

But it works from the command line when I try to run it like this:

let blockchain = new Blockchain();

var bla;

blockchain.getBlockHeightPromise().then(i => bla =  i);

How to make this work?

Aug 16, 2018 in Blockchain by slayer
• 29,040 points
12 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Try this code: 


// Add new block

    async addBlock(newBlock) {

        var bla = await this.getBlockHeightPromise();

        console.log('bla: ' + bla);

    }

The function is not working when you call it within the class is because the console statement is executed before getBlogHeightPromise is finished.

Using await to wait for the asynchronous method getBlogHeightPromise solves this problem

answered Aug 16, 2018 by digger
• 27,620 points

Related Questions In Blockchain

0 votes
1 answer

Geth 'sendTransaction' not working

When transactions are put in a loop, ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 66,880 points
39 views
0 votes
1 answer

Is it possible to claim Bitcoin Cash from an exchange that does not support it?

You can't claim BCH without knowing private ...READ MORE

answered Jul 17, 2018 in Blockchain by ariaholic
• 7,320 points
23 views
+1 vote
2 answers
0 votes
1 answer

My first Hyperledger fabric network is not working

Delete as admin the channel-artifacts folder, down ...READ MORE

answered Aug 10, 2018 in Blockchain by Johnathon
• 9,050 points
57 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
77 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

OpenSSL i2o_ECPublicKey not working

When your right something in the buffer, ...READ MORE

answered Aug 17, 2018 in Blockchain by digger
• 27,620 points
50 views
0 votes
1 answer

Not receiving any data from the BitStamp API.

I saw a code online that was ...READ MORE

answered Aug 21, 2018 in Blockchain by digger
• 27,620 points
33 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.