0

I am calling a certain function in an orderer. These functions work asynchronously & do not wait for the process to complete but I want to make it synchronous. Please tell me how should do it.

Below is function

 getMetaData(url).then((a) => {
        console.log("final a="+a)
        return a
    });



function getMetaData(url) {
    console.log("In getmetadata");
    return new Promise((resolve, reject) => {
        request({ url, encoding: null }, (err, resp, buffer) => {
            console.log("In request response");
            pdf(buffer).then(function (data) {
                var mdata = data.metadata;
                var filtered = {}
                for (key in mdata._metadata) {
                    if (key.match(/^pdfx:Blockchain/)) filtered[key] = mdata[key];
                }
                if (Object.keys(filtered).length === 1) {
                    isMetaData = true
                }
                a.isMetaData = isMetaData
                resolve(a);
                console.log("a value set");

            });

        });
    });
}
console.log("In the end")

Below is the output

In getmetadata
In the end
In request response
a value set
final a=[object Object]

But i want In the end should be printed in last & it should wait for whole process to complete.

TechChain
  • 8,404
  • 29
  • 103
  • 228

0 Answers0