IBM / ibm-cos-sdk-js

ibm-cos-sdk-js
Apache License 2.0
38 stars 20 forks source link

async/await version of api? #69

Closed pdykes closed 4 years ago

pdykes commented 4 years ago

Looking at the current API, is there another version that is friendly to await/async function in node 10/12? Thanks

sandersrIBM commented 4 years ago

Hi @pdykes - I am afraid I do not understand your question. Can you provide a little more detail? Do you have a code snip of what you are trying to do / would like to see?

pdykes commented 4 years ago

Thanks,

A more modern model that allows to avoid the promise function is to use the async/await solution and provide a much simpler, modern api style vs. promises to be as externalized.

a) Good overview:

An async function can contain an await expression, that pauses the execution of the function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value. ... The purpose of async/await is to simplify the behavior of using promises.

Use of async / await enables the use of ordinary try / catch blocks around asynchronous code. The await keyword is only valid inside async functions. ... Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Net, code is much simper for a consumer and they could include that in their code with large code restructure.

https://javascript.info/async-await

b) Simple Example:

function resolveAfter2Seconds() { return new Promise(resolve => { setTimeout(() => { resolve('resolved'); }, 2000); }); }

async function asyncCall() { console.log('calling'); const result = await resolveAfter2Seconds(); console.log(result); // expected output: "resolved" }

asyncCall();

(The resolveAfter2Seconds can be in the library vs. in user space)

c) error handling very clean, compared to other approaches, e.g. can do the following:

(https://itnext.io/error-handling-with-async-await-in-js-26c3f20bc06a)

async function thisThrows() { throw new Error("Thrown from thisThrows()"); }

async function run() { try { var some_return_value = await thisThrows(); } catch (e) { console.error(e); } finally { console.log('We do cleanup here'); } }

run();

Net, just a very clean api model for modern node solution post Node 10/12. The code you have is fine, but it could be simpler for adopter to use using await for example...

These simpler examples really don't show how much of a boon to programmers it provides, the code in that location will await the response and the readability and maintainability compared to older node code is much cleaner.

In any event, just a thought. I'll start with the above Thanks!

sandersrIBM commented 4 years ago

Hi @pdykes. ibm-cos-sdk-js is a fork of https://github.com/aws/aws-sdk-js for AWS S3 API supported in IBM Cloud for Cloud Object Storage.

In some cases, our upstream fork releases features that require further testing and evaluation before we can certify compatibility. I welcome you to make this a feature request for https://github.com/aws/aws-sdk-js, your feedback will help us determine which features are prioritized for future releases.

sandersrIBM commented 4 years ago

Issue is a request for functionality that upstream repo does not currently support. Closing issue.

mriedem commented 3 years ago

It might already be supported, FYI:

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/calling-services-asynchronously.html#using-async-await

Though that section of the docs is pretty sparse on details.

huineng commented 3 years ago

works very well

const response: S3.ListBucketsOutput | undefined = await cos.listBuckets().promise();
pdykes commented 3 years ago

Here is another invocation I have found useful to integrate this package with await (JSON example):

        var cos_object_detail = {
            "Key": "Some_fixed_data" + "_" + query_virtual_reference,
            "Bucket": cos_scale_bucket
        };
         const COSdata = (await (cos_storage.getObject(cos_object_detail).promise())).Body.toString('utf-8');
         query_response_data.results = JSON.parse(COSdata);

Per the above, AWS seems to support this as shown in more recent versions of the package usage.