I only need one array from a map that returns two promises with arrays

Currently, I am working on optimizing the process of adding food and drink to an order simultaneously. To achieve this, I have created a promise that resolves when both actions are completed. These promises are then nested within another promise, which resolves when all orders are processed.


orders = [
  {'name': 'john'},
  {'name': 'sam'},
]

const p = orders.map((order) => {
    return Promise.all([
        add_food(order),
        add_drink(order),
    ])
})

return Promise.all(p)

function add_food(order){
    return Promise.resolve(order)
}

However, the issue I am facing is that the resulting array contains duplicated entries.


[
  [ "order_1", "order_1"],
  [ "order_2", "order_2"]
]

While this outcome is logical, I am looking for a solution to restructure the array to return:


[ "order_1", "order_1"]

Any suggestions on how to approach this problem?

Answer №1

Simply include .then(_ => order) right after Promise.all in order to retrieve the order once the promises for food and drink have been fulfilled:

const p = orders.map(order => {
  return Promise.all([
    add_food(order),
    add_drink(order),
  ]).then(_ => order)
})

return Promise.all(p)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for removing unnecessary debugging code from JavaScript when compiling or minifying your code

Back in the day, I would always include tons of debug code in my JavaScript app. However, I'm now searching for a method that will eliminate debug code during the compilation/minification phase. Does the world of JavaScript have something similar to ...

Are external JavaScript files cached in the same manner as images?

Is it possible for a single pagehandler script to be called from the browser cache when navigating between multiple pages that refer to it? ...

Automatically refreshing controller functionality in CodeIgniter

Greetings everyone, I'm looking for a way to automatically refresh a controller function every 5 seconds. Currently, I am using header('Refresh: 10.2'); within the controller function like this: public function delete() { heade ...

What is the best way to implement a loop that executes a recursive binary search to find n elements within an array using JAVA?

Is there a way to modify my code to print multiple numbers instead of just one? package binariarecursiva; import java.util.Scanner; /** * * @author User */ public class BinariaRecursiva { /** * @param args the command line arguments */ public stati ...

@keyframes shimmering-fade

I'm attempting to create a text animation effect (please see video) but I'm struggling to find the solution!! Can someone assist me with this? Should I use JavaScript for a better result? h1.fadeinone { animation: fadeinone 10s;} h1.fadeintwo ...

Transforming every function into a for loop using jQuery

Previously, I sought help from the knowledgeable individuals on stackoverflow to assist me in shifting a background image the correct distance during a mouseover event. The solution worked smoothly, however, I am now pondering the efficiency of utilizing t ...

Update the pageExtensions setting in Next.js to exclude building pages with the file extension *.dev.*

I am currently working on a project using Next.js version v12.3, and I have encountered an issue related to excluding page files with a *.dev.* extension from the build process. In my configuration file next.config.js, I have configured the pageExtensions ...

Error: The <Class> cannot be accessed until it has been initialized

I have a basic autoloader method that initializes and returns an instance of a class using require() The require statement includes some logic that requests information from a database and checks if the class exists in the filesystem. let elementClass = r ...

The regex string parameter in node.js is not functioning properly for matching groups

The String.prototype.replace() method documentation explains how to specify a function as a parameter. Specifying a string as a parameter The replacement string can contain special patterns for inserting matched substrings, preceding and following portion ...

Is it recommended to utilize CDN in Vue.js for optimal performance?

Currently facing a version compatibility issue between leaflet and leaflet-draw in my vuejs project. In light of this, I am seeking alternative solutions for map function editing such as adding polylines, copy and paste functions, and more. While I did com ...

Is there a method in Java for adding a byte array to a servlet outputstream without replacing the existing content?

Hey there! I'm currently using Apache POI to generate an Excel format file, but I'm not storing it on my disk. Instead, I have it stored as a byte array and then I'm using an output stream to write the byte array to the client so they can do ...

I'm having trouble getting Grunt Source Maps to function properly within the foundation-press theme

I'm struggling to enable source maps for the npm package grunt-sass. Here's a snippet from my Gruntfile.js: The issue lies in this line: sourceMap: true, at line 13 module.exports = function(grunt) { var jsApp = [ 'js/app.js' ...

Accessing external content within our webpage

Previously, I utilized iframes to display an external page within our asp.net page. However, I have now decided to explore alternative methods that do not involve iframes. My goal is to open an external page within our page using only a simple aspx page wi ...

Issue with VueUse useStorage function failing to update stored object properties

Having difficulty updating a property in an object stored in localStorage using the useStorage function. Inside the App.vue file: routeStore.query = { tab: 'products', subTab: 1, search: '', article: '', } console.log ...

A JavaScriptSerializer counterpart to XmlAttribute/XmlElement

Is there a similar Attribute that can be used on object Properties within a .NET Class to achieve the same functionality as XmlElement or XmlAttribute? [XmlRoot("objects")] public class MyObjects: List<MyObject> { } [XmlRoot("object")] public class ...

Find the height of the viewport using jQuery, subtracting (n) pixels

Apologies if the topic seems puzzling, I couldn't find a better way to explain it. I utilized jQuery to adjust the height of a div to match the size of the viewport. var slidevh = function() { var bheight = $(window).height(); $(".container" ...

A function that retrieves the empty values from an array and returns undefined if there

After undergoing a time-consuming process, the sample below shows that the array values are returned empty. function myFunction() { let myArray = []; let pastArray = [1, 2, 6, 7, 8, 1, 9, 6, 0] pastArray.forEach(item =>{ setTimeout(function(){ myA ...

Turbolinks gem causing ShareThis to malfunction

After adding the turbolinks and jquery-turbolinks gems to my project, I noticed that my ShareThis button no longer pops up when clicked. The ShareThis scripts currently included in my application.html.erb head are: <script type="text/javascript">va ...

Guide to attaching and displaying an image on a Three.js map

Currently, I have a 3D map loaded with three.js that includes mouse interaction. I've managed to place an image on the map using absolute positioning, but unfortunately, as I move the map around, the image stays stationary. Does anyone know how I can ...

Methods for applying multiple styles within a div using the Document Object Model

Is there a way to add multiple style attributes using DOM `setAttribute` in JavaScript? I've tried doing it but it doesn't seem to work. Can someone provide guidance on how to achieve this? var modify = document.getElementById('options&apo ...