小编Fee*_*ech的帖子

Axios,Typescript和Promises

这可能更像是一个TypeScript问题.我有一个REST方法,通过Vue操作在我的http库中调用.我想使用resolve()方法返回类型化的数组,但除非我在action.ts"then"方法中进行转换,否则我会收到类型{}中不存在'.length'

有没有办法不必将结果重新转换为我的类型数组,因为它已经在http.ts方法中完成了?

http.ts(部分)

getapps() {        
        return new Promise((resolve, reject) => {
            this.axios.post('/account/getapps').then((response) => {
                resolve(response.data as DomainAppType[]);
            }, (err) => {
                reject(err);
            });
         });
    }
Run Code Online (Sandbox Code Playgroud)

action.ts

import { DomainAppType } from '../models/domainApps';

var actions = {
    LOGIN: function ({ commit }, params) {
        http.getapps(params.email, params.password).then(apps => {
            var appList = <DomainAppType[]>apps;
            console.log(appList.length);
        }).catch((err) => {
            console.log(JSON.stringify(err));
        })

      }
}
export default actions
Run Code Online (Sandbox Code Playgroud)

typescript vue.js axios

6
推荐指数
1
解决办法
6565
查看次数

暂停同步REST调用,直到第一个完成

我们有一个类似于以下的REST API方法:

List<item> GetItems(int AccountID)
{
    var x = getFromCache(AccountID);
    if(x==null)
    {
        x = getFromDatabase(AccountID);
        addToCache(AccountID, x);
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

这是一个相当昂贵的方法,有一些复杂的数据库调用,我们有一个常见的情况,数百名具有相同AccountId的用户几乎会同时进行调用(他们都通过广播通知).

在该方法中,我们将结果集缓存10秒,因为对于在该窗口内发出请求的每个人来说,近期结果都很好.但是,由于它们都同时进行调用(同样,对于特定的AccountID),缓存永远不会预先填充,因此每个人最终都会进行数据库调用.

所以我的问题是,在该方法中,如何暂停特定accountId的所有传入请求并使它们等待第一个结果集完成,以便其余的调用可以使用缓存的结果集?

我已经阅读了一些关于Monitor.Pulse和Monitor.Lock的内容,但是每个accountId锁的实现让我感到安慰.任何帮助将非常感激.

c# rest multithreading block pulse

5
推荐指数
1
解决办法
435
查看次数

标签 统计

axios ×1

block ×1

c# ×1

multithreading ×1

pulse ×1

rest ×1

typescript ×1

vue.js ×1