jQuery承诺提供summernote提示

我正在努力从数据库中获取Summernote提示的实时用户列表,但是当使用异步时,它只会失败,但是如果异步关闭它会导致UI冻结…显然不是UX的最佳选择。

$(document).ready(function() { $('.editor').summernote({ height: 300, hint: { match: /\B@(\w*)$/, users: function(keyword) { var result = data; $.ajax({ url: '/users/' + keyword, type: 'get', async: false //This works but freezes the UI }).done(function(data) { result = data; //Set the result to the returned json array }); return result; }, search: function (keyword, callback) { callback(this.users(keyword)); //callback must be an array }, content: function (item) { return '@' + item; } } }); }); 

如何在不摔倒的情况下让异步工作? 我相信它与承诺有关,但不确定。

不要callbackusers需要从done函数调用它。

 $(document).ready(function() { $('.editor').summernote({ height: 300, hint: { match: /\B@(\w*)$/, users: function(keyword, callback) { $.ajax({ url: '/users/' + keyword, type: 'get', async: true //This works but freezes the UI }).done(callback); }, search: function (keyword, callback) { this.users(keyword, callback); //callback must be an array }, content: function (item) { return '@' + item; } } }); });