简体   繁体   中英

Getting a value after promise resolved

I'm trying to understand Promises as best as I can.

I was wondering if there's a way of returning the data after the promise had been resolved without chaining as much as I have

Basically i was trying to push a new location into the locs array.

loc.service.js:

export const locService = {
  getLocs,
};

const locs = [
  { name: 'Greatplace', lat: 32.047104, lng: 34.832384 },
  { name: 'Neveragain', lat: 32.047201, lng: 34.832581 },
];

function getLocs() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(locs);
    }, 100);
  });
}

map.service.js:

      const place = prompt('Enter place name');
      if (!place) {
        alert('Please enter a valid input');
      } else {
        locService.getLocs().then(locService.setLoc(place, event.latLng));
        locService.getLocs().then((locs) => {
          storageService.save(PLACE_KEY, locs);

thanks a bunch

Yes. check async/await syntax.

MDN doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM