Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

javascript - [[PromiseState]]: "rejected" in loop

I am using react in javascript and I try to encrypt array elements using a loop, then I want to push each result in an element of the new array, I use this code:

   var eccrypto = require("eccrypto");
    
   var privateKey= Buffer.from([238, 239, 199, 101, 188, 134, 13, 13,195, 172, 125, 168, 225, 189, 72, 148, 225, 200, 127, 218, 204, 11, 150, 146, 180, 243, 195, 109, 200, 119, 50, 20],'hex');
   


  let array = [2,3,5];
  let array2=[];


    for (let b of array) {
          
          const a= eccrypto.sign(privateKey,b);
          array2.push(a);

        }
        console.log(array2);

the problem is it rejected in the second and fourth elements. The result is like this: enter image description here

and the error message: enter image description here

I tried to use async/await as a lot of the previous problems solved by using it, but I get the same problem.

I'll appreciate your help, thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to do following way

import eccrypto  from 'eccrypto'
import crypto from 'crypto'

export default function App() {
  let array = [2,3,5];
  
let array2=[];
 
  var privateKey = eccrypto.generatePrivate();
  for (let b of array) {
    var msg = crypto.createHash("sha256").update(b).digest();
    eccrypto.sign(privateKey, msg).then(function(a) {
      array2.push(a);
      
    });
    
  }
  console.log(array2);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...