• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {launch} from "std/concurrency"
17import {Job} from "std/core"
18
19type Cb = (p: int) => int;
20
21let fs: Cb[] = []
22
23function foo(i: int): Cb {
24    return fs[i]
25}
26
27function main() {
28    fs = [
29        (p: int): int => p + 1,
30        (p: int): int => p + 2,
31        (p: int): int => p + 3,
32        (p: int): int => p + 4,
33        (p: int): int => p + 5,
34        (p: int): int => p + 6,
35        (p: int): int => p + 7,
36        (p: int): int => p + 8,
37        (p: int): int => p + 9,
38        (p: int): int => p + 0,
39    ]
40
41    // array of promises
42    let ps: Object[] = new Object[10]
43    for (let i = 0; i < 10; i++) {
44        ps[i] = launch<Cb, (i: int) => Cb>(foo, i)
45    }
46
47    let cnt = 0
48    for (let i = 9; i >= 0; i--) {
49        cnt += ((ps[i] as Job<Cb>).Await())(i) // <- complains on this line
50    }
51    assertEQ(cnt, 90);
52}
53