async/await vs promises

by Vladymyr Shevchuk

JavaScript

const asyncFunc = async () => {
    const response = await fetch('https://jsonplaceholder.typicode.com/posts/1')
    const data = await response.json();

    // I have access to a "response" and "data" objects in the same scope
    console.error('response', response);
    console.error('data', data);
    return data;
}

const wrappedAsyncFunc = () => {
  return (async () => {
		return await asyncFunc();
  })()
  .catch(error => {
  	console.error('Error: ', error);
  })
};

const catchify = func => {
	return func().catch(error => {
  	console.error('Error: ', error);
  })
};

const promiseFunc = () => {
	fetch('https://jsonplaceholder.typicode.com/posts/1')
  	.then(response => {
    	console.error('response', response);
      return response.json();
    })
    .then(data => {
    	// It's impossible to get access to "response" object without extra variable
      console.error('data', data);
    })
    .catch(error => {
    	console.error('Error: ', error);
    });
}

wrappedAsyncFunc();
catchify(asyncFunc);