diff --git a/src/sql/platform/query/common/gridDataProvider.ts b/src/sql/platform/query/common/gridDataProvider.ts index d1969233dfc7..c4a71694d47a 100644 --- a/src/sql/platform/query/common/gridDataProvider.ts +++ b/src/sql/platform/query/common/gridDataProvider.ts @@ -43,13 +43,13 @@ export interface IGridDataProvider { } export async function getResultsString(provider: IGridDataProvider, selection: Slick.Range[], includeHeaders?: boolean): Promise { - let headers: Map = new Map(); // Maps a column index -> header - let rows: Map> = new Map(); // Maps row index -> column index -> actual row value + let headers: Map = new Map(); // Maps a column index -> header + let rows: Map> = new Map(); // Maps row index -> column index -> actual row value const eol = provider.getEolString(); // create a mapping of the ranges to get promises - let tasks = selection.map((range, i) => { - return async () => { + let tasks: (() => Promise)[] = selection.map((range) => { + return async (): Promise => { let startCol = range.fromCell; let startRow = range.fromRow; @@ -88,22 +88,24 @@ export async function getResultsString(provider: IGridDataProvider, selection: S }; }); - if (tasks.length > 0) { - let p = tasks[0](); - for (let i = 1; i < tasks.length; i++) { - p = p.then(tasks[i]); - } - await p; - } + // Set the tasks gathered above to execute + let actionedTasks: Promise[] = tasks.map(t => { return t(); }); + + // Make sure all these tasks have executed + await Promise.all(actionedTasks); + + const sortResults = (e1: [number, any], e2: [number, any]) => { + return e1[0] - e2[0]; + }; + headers = new Map([...headers].sort(sortResults)); + rows = new Map([...rows].sort(sortResults)); let copyString = ''; if (includeHeaders) { copyString = [...headers.values()].join('\t').concat(eol); } - const rowKeys = [...headers.keys()].sort(); - - rows = new Map([...rows.entries()].sort()); + const rowKeys = [...headers.keys()]; for (let rowEntry of rows) { let rowMap = rowEntry[1]; for (let rowIdx of rowKeys) {