我有一个函数可以执行某些逻辑,然后运行成功回调函数或失败回调函数。
以下是代码:
package com.demo.search.repository;
import com.demo.model.Book;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
/**
* Test things on {@link BookSearchRepository}
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class BookSearchRepositoryTest {
@Autowired
private BookSearchRepository bookRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Before
public void before() {
elasticsearchTemplate.deleteIndex(Book.class);
elasticsearchTemplate.createIndex(Book.class);
elasticsearchTemplate.putMapping(Book.class);
elasticsearchTemplate.refresh(Book.class);
}
@Test
public void shouldSaveBook(){
Book book = new Book();
book.setBookId("1");
book.setTitle("Learning Scala");
Book indexedBook = bookRepository.save(book);
assertThat(indexedBook, is(notNullValue()));
assertThat(indexedBook.getBookId(), is(book.getBookId()));
assertThat(bookRepository.findOne("1"), is(notNullValue()));
}
}
如何将some_function() ->
ArgumentsForCallback = [],
check_some_condition(Input, fun success_callback/1, fun fail_callback/1).
传递给回调?
答案 0 :(得分:7)
有两种方法:
将参数传递给check_some_condition
并使该函数将参数发送给回调:
check_some_condition(Input, ArgumentsForCallback, Success, Fail) ->
Success(ArgumentsForCallback).
some_function() ->
ArgumentsForCallback = [],
check_some_condition(Input, ArgumentsForCallback, fun success_callback/1, fun fail_callback/1).
将匿名函数发送到check_some_condition
:
check_some_condition(Input,
fun() -> success_callback(ArgumentsForCallback) end,
fun() -> fail_callback(ArgumentsForCallback) end).