Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating js-fetch code to have an asyncAwaitEnabled option. #717

Merged
merged 3 commits into from
Jul 20, 2023

Conversation

kevinswiber
Copy link
Member

@kevinswiber kevinswiber commented Jul 14, 2023

Fixes #390.

Also updating request timeout to properly use an AbortController signal.

Sample output with asyncAwaitEnabled set to true:

const myHeaders = new Headers();
myHeaders.append("my-sample-header", "Lorem ipsum dolor sit amet");
myHeaders.append("not-disabled-header", "ENABLED");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

try {
  const response = await fetch("https://mockbin.org/request", requestOptions);
  const result = await response.text();
  console.log(result)
} catch(error) {
  console.error(error);
};

Sample output with asyncAwaitEnabled set to true and requestTimeout set to 3000:

const controller = new AbortController();
const timerId = setTimeout(() => controller.abort(), 3000);
const myHeaders = new Headers();
myHeaders.append("my-sample-header", "Lorem ipsum dolor sit amet");
myHeaders.append("not-disabled-header", "ENABLED");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  signal: controller.signal,
  redirect: "follow"
};

try {
  const response = await fetch("https://mockbin.org/request", requestOptions);
  const result = await response.text();
  console.log(result)
} catch (error) {
  console.error(error);
} finally {
  clearTimeout(timerId);
};

Sample output with asyncAwaitEnabled set to false and requestTimeout set to 3000:

const controller = new AbortController();
const timerId = setTimeout(() => controller.abort(), 3000);
const myHeaders = new Headers();
myHeaders.append("my-sample-header", "Lorem ipsum dolor sit amet");
myHeaders.append("not-disabled-header", "ENABLED");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  signal: controller.signal,
  redirect: "follow"
};

fetch("https://mockbin.org/request", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error))
  .finally(() => clearTimeout(timerId));

Copy link

@agrwl-harsh agrwl-harsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@VShingala VShingala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! The new snippet looks much cleaner. 🚀

@VShingala VShingala merged commit 2411e41 into postmanlabs:develop Jul 20, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Let javascript-Fetch also have an option to generate async/await code.
3 participants