It is currently 8 June 2025, 20:40 Advanced search

yeld vs await

Domande e risposte su come utilizzare Instant Developer Cloud al meglio

yeld vs await

Postby mcdok » 10 December 2020, 17:08

Ciao, sto cercando di utilizzare un pacchetto esterno sul node installato su Inde Cloud, il problema è che negli script viene utilizzata in vari punti la keyword await:

Code: Select all
await gateway.connect(...)

questa non viene riconosciuta da Inde Cloud e se provo a sostituirla con:

Code: Select all
yeld gateway.connect(...)

mi da errore: yeld non può essere usato su un metodo sconosciuto.

come ne esco?
mcdok
 
Posts: 855
Joined: 19 July 2011, 16:18

Re: yeld vs await

Postby lucabaldini » 22 December 2020, 16:19

Al momento non credo tu possa chiamare direttamente un metodo con await nei tuoi metodi... non ho mai provato ma ho come il sospetto che non sia possibile.

Per poter usare await in un tuo metodo questo deve essere definito con "async".

In pratica il tuo metodo deve essere definito così:

Code: Select all
async function mioMetodo() {
  ...
  await metodoConAwait("Hello");
  ...
}


O qualcosa del genere.

Quindi, ci sono varie soluzioni:
1) puoi caricare un file javascript (risorsa server) dove "adatti" le chiamate ai metodi async e ne permetti la chiamata tramite callback così da poter essere usate dentro al tuo codice
2) scrivere il codice senza await. Una chiamata del tipo:

Code: Select all
  await metodoAsync('a','b','c');
  ...


dovrebbe potersi scrivere così:

Code: Select all
  metodoAsync('a','b','c').then(function() {
      ...
  });
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 4990
Joined: 1 October 2010, 17:03
Location: Bologna

Re: yeld vs await

Postby lucabaldini » 22 December 2020, 16:19

Per essere sicuro dovrei vedere il codice che devi chiamare e provare a cambiarlo usando direttamente la Promise...
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 4990
Joined: 1 October 2010, 17:03
Location: Bologna

Re: yeld vs await

Postby mcdok » 22 December 2020, 17:18

Ciao, le funzioni in questione fanno parte del pacchetto fabric-network e non posso ridefinirle.

Nei vari esempi di utilizzo vengono appunto richiamate con await. Non ho ancora appurato se ci sono altre possibilità.
Questo il codice di esempio:
Code: Select all
     const gateway = new Gateway();
     await gateway.connect(ccp, { wallet, identity: 'admin' , discovery: {enabled: true, asLocalhost:false }});
     console.log('Connected to Fabric gateway.');


In assistenza recentemente Lanzi mi ha consigliato di provare a richiamarle utilizzando le funzioni di callback.
Tipo:
Code: Select all
  let fSuccess = function (res) {
    // op success
  };
  let fError = function (err) {
    // op error
  };
  gateway.connect(ccp, {wallet, identity : 'admin', discovery : {enabled : true, asLocalhost : false} }, fSuccess, fError);


Devo fare qualche prova al riguardo appena ne ho il tempo.
mcdok
 
Posts: 855
Joined: 19 July 2011, 16:18

Re: yeld vs await

Postby mcdok » 23 December 2020, 12:01

Ho fatto una prova veloce ma ottengo in entrambi i modi sempre lo stesso errore:

(node:1891) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, mkdir '/mnt/disk/IndeRT/ide/app/server/_idwallet'

quindi probabilmente c'è qualcosa che sbaglio nell'impostare la directory del wallet che contiene le credenziali sul server di sviluppo.
mcdok
 
Posts: 855
Joined: 19 July 2011, 16:18

Re: yeld vs await

Postby lucabaldini » 23 December 2020, 13:58

Non capisco quel path. Che prova hai fatto esattamente?
Non esiste quel path...
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 4990
Joined: 1 October 2010, 17:03
Location: Bologna

Re: yeld vs await

Postby mcdok » 28 December 2020, 15:35

Ti mostro il codice di esempio e quello che sto cercando di riscrivere in Inde Cloud:

esempio:
Code: Select all
const { FileSystemWallet, Gateway } = require('fabric-network');
const fs = require('fs');
const path = require('path');

     // Parse the connection profile. This would be the path to the file downloaded
     // from the IBM Blockchain Platform operational console.
     const ccpPath = path.resolve(__dirname, 'connection.json');
     const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));


inde cloud:
Code: Select all
  const fabric = require('fabric-network');
  const path = require('path');

  var f = app.fs.file($connection, App.Fs.internalType.resource);
  yield f.open();
  var s = yield f.readAll();
  yield f.close();
  const ccp = JSON.parse(s);


e fin qui funziona, poi passa a costruire l'oggetto wallet che contiene le credenziali d'accesso, ho creato nello spazio dati dell'app nell'IDE di inde cloud la dir _idwallet con la sotto-dir admin al cui interno ci sono i file di chiave e certificato:

esempio:
Code: Select all
     // Configure a wallet. This wallet must already be primed with an identity that
     // the application can use to interact with the peer node.
     const walletPath = path.resolve(__dirname, '_idwallet');
     const wallet = new FileSystemWallet(walletPath);


inde cloud:
Code: Select all
  const walletPath = path.resolve(__dirname, '_idwallet');
  const myWallet = new fabric.FileSystemWallet(walletPath);


a questo punto chiama la funzione per la creazione del gateway a cui passa le info del file di configurazione e del wallet e che utilizza await:

esempio:
Code: Select all
     // Create a new gateway, and connect to the gateway peer node(s). The identity
     // specified must already exist in the specified wallet.
     const gateway = new Gateway();
     await gateway.connect(ccp, { wallet, identity: 'admin' , discovery: {enabled: true, asLocalhost:false }});


inde cloud:
Code: Select all
  const gateway = new fabric.Gateway();
  gateway.connect(ccp, {wallet: myWallet, identity : 'admin', discovery : {enabled : true, asLocalhost : false} }, fSuccess, fError);


e a questo punto ottengo l'errore relativo alla directory del wallet.
Last edited by mcdok on 28 December 2020, 16:01, edited 1 time in total.
mcdok
 
Posts: 855
Joined: 19 July 2011, 16:18

Re: yeld vs await

Postby mcdok » 28 December 2020, 15:40

Ulteriori informazioni:

la Gateway.connect è definita così:
Code: Select all
export class Gateway {
  constructor();
  public connect(config: Client | string | object, options: GatewayOptions): Promise<void>;
  ...
}

async connect(config, options) {
  ..
}


e purtroppo nel modulo in cui è definita ci sono riferimenti all'altro pacchetto fabric-client che non siamo riusciti ad installare durante l'assistenza:

Code: Select all
import { Channel, ChannelPeer, TransactionId, User } from 'fabric-client';
import Client = require('fabric-client');


quindi probabilmente anche risolvendo il primo errore si fermerebbe comunque per l'assenza dell'altro pacchetto...
mcdok
 
Posts: 855
Joined: 19 July 2011, 16:18

Re: yeld vs await

Postby lucabaldini » 29 December 2020, 8:07

Riguardo il path, l'"errore" è nell'usare la macro __dirname

Quella contiene il "path" del file che è in esecuzione in quel momento... non è un "path" valido per le app perché andrebbe ad interferire con le directory dei sorgenti.

Occorre usare un path calcolato meglio, usando le funzioni della libreria FS.

Riguardo all'altro problema... credo sia difficile gestire una cosa così via forum... per esempio capire perché il pacchetto fabric-client non si installi via forum non è semplice. Bisogna provare ad installarlo su un server e vedere che errori vengono generati per capire cosa non funziona.
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 4990
Joined: 1 October 2010, 17:03
Location: Bologna

Re: yeld vs await

Postby lucabaldini » 29 December 2020, 8:08

Riguardo la connect vedo che è una Promise quindi si può chiamare anche senza await tramite il costrutto then.catch
User avatar
lucabaldini
Pro Gamma
Pro Gamma
 
Posts: 4990
Joined: 1 October 2010, 17:03
Location: Bologna

Next

Return to Tips & Tricks - Cloud

Who is online

Users browsing this forum: No registered users and 7 guests