Some cloud apis are considered to be ‘long running’, which means you wont get the result right away. Rather the result is stored and you can retrieve it later. There is an api for doing all that, so you can of course use that. However, many of the Node API libraries have got that complication covered by returning a promise to the result. This article will use the Video Intelligence API as an example.
Lets reduce it to this, where request contains the parameters for the video intelligence annotation request
const operationResult = await doLong(request);
doLong
The Api makes it super simple to submit the request and get a promise to the final result
// manage a long running annotation operation const doLong = async (request) => { // its a long running operation const [operation] = await viClient.annotateVideo(request); console.log ('annotating'); // when done, retrieve the result const [operationResult] = await operation.promise(); console.log('getting result'); return operationResult; };
- The api returns an array, the first element of which is the operation object
- You can extract a promise from the operation object – again its an array, and the first element of it is the result of the long running operation when resolved.
Done
And now you can extract the operation results from that long running promise resolution. Again, it’s an array, but you only want the first element.
const [annotations] = operationResult.annotationResults;