How to complete generateHash function?

block.js

In block.js, there is a generateHash function. I'm unable to use Promises to calculate the hash asynchronously and update the hash property.

class Block {
constructor(data){
            this.id = 0;
            this.nonce = 144444;
            this.body = data;
            this.hash = "";
        }

generateHash() {
// Use this to create a temporary reference of the class object
let self = this;

//Implement Promises here

}
}

// Exporting the class Block to be reuse in other files
module.exports.Block = Block;

app.js

In app.js, the generateHash function is called and Promises are handled which is pretty clear to me:

const BlockClass = require(‘./block’);

/**

  • Creating a block object
    */
    const block = new BlockClass.Block(“Test Block”);

// Generating the block hash
block.generateHash().then((result) => {
console.log(Block Hash: ${result.hash});
console.log(Block: ${JSON.stringify(result)});
}).catch((error) => {console.log(error)});


#node-js #javascript #blockchain

3 Likes7.65 GEEK