Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda