I
I
ivandao2021-07-15 17:02:55
JavaScript
ivandao, 2021-07-15 17:02:55

How to make it easier to write a function?

const f = (data) => {
      return new Promise((resolve) => {
        resolve({
          data: data
        });
      }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ukolov, 2021-07-15
@ivandao

Simplify by removing the meaningless promise.
There is nowhere easier:
const f = (data) => ({data});

A
Aetae, 2021-07-15
@Aetae

Alexey Ukolov is right.
But if for some reason you need a promise (for a chain thenthere, or something else), then there is a built-in helper for this: You can also use it with the same success:
const f = data => Promise.resolve({data});
const f = async data => ({data});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question