Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
how to apply javascript promises for async data hashing

In an online blockchain developer course I am participating in, one prerequisite was javascript object oriented & async programming, which I do not have experience in. However, having some experience in programming, I figured I would just learn as I go.

A practice activity (not a graded one, those I can not get help on) on blockchain hashing wants us to create an async hashing function using promises to hash data with SHA256. There are 2 main files: app.js, and block.js .

app.js (this was given to us, it is the main file we run):

/**
 * Importing the Block class
 */
//
const BlockClass = require('./block.js');

/**
 * 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)});

/**
 * Step 3: Run the application in node.js
 * 
 */

// From the terminal: cd into Project folder
// From the terminal: Run node app.js to run the code

block.js ( the arrows indicate the part I added myself, everything else was given.

/**
 * Import crypto-js/SHA256 library
 */
const SHA256 = require('crypto-js/sha256');

/**
 * Class with a constructor for block 
 */
class Block {

    constructor(data){
        this.id = 0;
        this.nonce = 144444;
        this.body = data;
        this.hash = "";
    }
    
    /**
     * Step 1. Implement `generateHash()`
     * method that return the `self` block with the hash.
     * 
     * Create a Promise that resolve with `self` after you create 
     * the hash of the object and assigned to the hash property `self.hash = ...`
     */
    // 
    generateHash() {
        // Use this to create a temporary reference of the class object
        let self = this;

> //Implement your code here
>         self.hash = SHA256(JSON.stringify(self.body));
>           const promise = new Promise(function(myResolve,myReject){
>           if(self.hash = SHA256(JSON.stringify(self.body))){
>           myResolve(self);
>           }else{
>            myReject(Error("It Broke")); 
>           }
>         
>         });
>       
>           promise.then(
>         function(result){this.hash = result.hash;},
>         function(error){console.log(error);}
>         );
>     }
}

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

Utilizing online resources, I do sort of understand promises, but not really- and definitely not how to apply them here. I was hoping I could get some help on this.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!